Click here to Skip to main content
15,885,869 members
Articles / Programming Languages / C#

Asynchronous Stream Reader and Writer with Progress Support

Rate me:
Please Sign up or sign in to vote.
4.38/5 (8 votes)
21 Apr 2009CPOL2 min read 38.7K   816   38   5
AsyncStreaming
AsyncStreaming - Click to enlarge image

Introduction

AsyncStreaming is a class library for asynchronous stream reading and writing. AsyncStreaming supports progress on reading and writing.

Structure

Common classes that use both AsynStreamReader and AsyncStreamWriter:

  • AsyncStreamException - This class represent errors that occur during asynchronous streaming.
  • AsyncStreamStateChangeArgs - This class provides data for the on state change event.
  • AsyncStreamErrorEventArgs - This class provides data for the on error event.
  • AsyncStreamState - This enumeration specifies identifiers to indicate the state of AsyncStreaming.

Possible states:

  • None
  • Ready - This state occurs when stream is ready to start.
  • Started - This state occurs when stream is started.
  • Paused - This state occurs when stream is paused.
  • Stopped - This state occurs when stream is stopped.
  • Finished - This state occurs when stream is finished.
  • Error - This state occurs when a stream exception has happened.

AsyncStreamReader

read_progress.jpg - Click to enlarge image
  • BaseAsyncStreamReader - Base functionality for asynchronous reading. So you can easily use this class to implement base functionality in your own reading class.
  • AsyncStreamReader - Class that extends from BaseAsyncStreamReader class and adds constructors.

Public methods:

  • StartRead() - Starts an asynchronous read operation
  • PauseRead() – Pause an asynchronous read operation
  • ResumeRead() – Resume a paused asynchronous read operation
  • StopRead() – Stops an asynchronous read operation

Events:

  • OnReadedBytes – Occurs when the progress is increased by one percent
  • OnEndRead – Occurs when all the bytes are read
  • OnError – Occurs when an AsyncStreamExcpetion happens
  • OnStateChanged – Occurs when state of AsyncStreamReader is changed

Constructor:

C#
/// <summary>
/// Implements a System.IO.TextReader that reads characters from a byte stream
/// in a particular encoding.
/// </summary>
/// <param name="path">The complete file path to be read.</param>
public AsyncStreamReader(string path)

AsyncStreamWriter

write_progress.jpg - Click to enlarge image
  • BaseAsyncStreamWriter – Base functionality for asynchronous writing. So you can easily use this class to implement base functionality in your own writing class.
  • AsyncStreamWriter – Class that extends from BaseAsyncStreamWriter class and adds constructors.

Public methods:

  • StartWrite() – Begins an asynchronous write operation
  • PauseWrite() – Pauses an asynchronous write operation
  • ResumeWrite() – Resumes a paused asynchronous write operation
  • StopWrite() – Stops an asynchronous write operation

Events:

  • OnStateChanged – Occurs when state of AsyncStreamWriter is changed
  • OnWritedBytes – Occurs when the progress is increased by one percent
  • OnEndWrite – Occurs when all bytes are written
  • OnError - Occurs when an AsyncException happens

Constructors:

C#
/// <summary>
/// Initializes a new empty instance of the AsyncStreaming.AsyncStreamWriter class.
/// </summary>
public AsyncStreamWriter()
C#
/// <summary>
/// Initializes a new instance of the AsyncStreaming.AsyncStreamWriter 
/// class for the specified
/// file on the specified path, using the default encoding.
/// </summary>
/// <param name="outputPath">The complete file path to write to. 
/// Path can be a file name.</param>
public AsyncStreamWriter(string outputPath) 
C#
/// <summary>
/// Initializes a new instance of the AsyncStreaming.AsyncStreamWriter 
/// class for the specified
/// file on the specified path, using the default encoding.
/// </summary>
/// <param name="outputPath">The complete file path to write to. 
/// Path can be a file name.</param>
/// <param name="buffer">The string to write to the stream. 
/// If value is null, nothing is written.</param>
/// <param name="encoding">The string encoding to use.</param>
public AsyncStreamWriter(string outputPath, string buffer, Encoding encoding) 
C#
/// <summary>
/// Initializes a new instance of the AsyncStreaming.AsyncStreamWriter 
/// class for the specified
/// file on the specified path, using the default encoding.
/// </summary>
/// <param name="outputPath">The complete file path to write to. 
/// Path can be a file name.</param>
/// <param name="buffer">The string to write to the stream. 
/// If value is null, nothing is written.</param>
public AsyncStreamWriter(string outputPath, string buffer)
C#
/// <summary>
/// Initializes a new instance of the AsyncStreaming.AsyncStreamWriter 
/// class for the specified
/// file on the specified path, using the default encoding.
/// </summary>
/// <param name="outputPath">The complete file path to write to. 
/// Path can be a file name.</param>
/// <param name="buffer">An array of bytes. 
/// This method copies count bytes from buffer to the current stream.</param>
public AsyncStreamWriter(string outputPath, byte[] buffer)

History

  • 21st April, 2009: Initial post

License

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


Written By
Web Developer
Slovenia Slovenia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionConvert to VS 2005? Pin
Raffy Ibasco1-Oct-09 18:21
Raffy Ibasco1-Oct-09 18:21 
GeneralArticle Pin
stixoffire27-Apr-09 17:50
stixoffire27-Apr-09 17:50 
Some points about the article - consider the audience might not have the understanding that you do and are learning. [That would be me.]
In your introduction:
What would be the purpose of an AsyncStreamReader / Writer - why would you want to use this - for example in what situation or problem would this nifty bit of code someone solve. BTW your English is fine not pefect but easily understandable..

Other than that everything is cool..thanks for sharing the code.
GeneralNice little program not so good article Pin
Paw Jershauge21-Apr-09 21:28
Paw Jershauge21-Apr-09 21:28 
GeneralRe: Nice little program not so good article Pin
mbostjan23-Apr-09 7:35
mbostjan23-Apr-09 7:35 
GeneralRe: Nice little program not so good article Pin
Paw Jershauge23-Apr-09 20:24
Paw Jershauge23-Apr-09 20:24 

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

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