Click here to Skip to main content
Licence CPOL
First Posted 14 Apr 2009
Views 16,184
Downloads 246
Bookmarked 32 times

Asynchronous stream reader with progress bar support

By | 14 Apr 2009 | Article
Asynchronous stream reader with progress bar support.

Introduction

This piece of code does async stream reading enabling you to use a progress bar in your application. On every byte read, it fires an event that contains the percentage of current read progress.

This is just an idea of how to make an async reader, so if you have any suggestions on how make it better or faster, let me now.

Using the code

The following are the AsyncStreamReader events:

  • EventHandler<AsyncReadEventArgs> OnReadedBytes - Occurs when the byte is read from the System.IO.StreamReader, and contains the percentage of total read bytes.
  • EventHandler<AsyncReadEventArgs> OnEndRead - Occurs when all bytes are read from the System.IO.StreamReader and contains the byte array of the read content.
  • EventHandler<AsyncReadErrorEventArgs> OnError - Occurs when an AsyncStream.AsyncExcpetion happens and contains the exception.
  • EventHandler<AsyncStreamStateChangeArgs> OnStateChanged - Occurs when the state of AsyncStream.AsyncStreamReader changes and contains the current state.

The following are the AsyncStreamReader public properties:

  • AsyncStreamState State - Specifies the identifiers to indicate the the state of AsyncStream.AsyncStreamReader.
  • string Path - Gets the complete file path to be read.

The following are the AsyncStreamReader pubic methods:

  • BeginRead() - Begins an asynchronous read operation and sets the state to Started.
  • StopRead() - Stops an asynchronous read operation and sets the state to Stopped.
  • PauseRead() - Pauses an asynchronous read operation and sets the state to Paused.
  • ResumeRead() - Resumes a paused asynchronous read operation and sets the state back to Started.

screenshot_0.jpg

screenshot_1.jpg

This is how we create a new asynchronous stream:

AsyncStreamReader reader = new AsyncStreamReader(fileName);
reader.OnReadedBytes += new EventHandler<AsyncReadEventArgs>(reader_OnReadedBytes);
reader.OnEndRead += new EventHandler<AsyncReadEventArgs>(reader_OnEndRead);
reader.OnStateChanged += 
       new EventHandler<AsyncStreamStateChangeArgs>(reader_OnStateChanged);
reader.OnError += new EventHandler<AsyncReadErrorEventArgs>(reader_OnError);
reader.BeginRead();

Here is the OnReadedBytes event:

void reader_OnReadedBytes(object sender, AsyncReadEventArgs e)
{
   int percents = e.PercentReaded; // percents of readed bytes
   bool isComplete = e.IsComplete; // is read completed
   long bytesReaded = e.BytesReaded; // number of readed bytes
   long length = e.Length; // total length of bytes to read;
   byte[] bytes = e.Result; // all readed bytes
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

mbostjan

Web Developer

Slovenia Slovenia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralFYI - Speed Issue in AsyncReader PinmemberEtherealMonkey18:41 6 Dec '10  
GeneralMy vote of 4 PinmemberEtherealMonkey17:21 6 Dec '10  
QuestionTitle? PinmemberAdrian Cole9:28 14 Apr '09  
Generalworker.Abort(); PinmemberMarc Leger6:17 14 Apr '09  
GeneralRe: worker.Abort(); Pinmembermbostjan6:57 14 Apr '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 14 Apr 2009
Article Copyright 2009 by mbostjan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid