Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Asynchronous Stream Reader and Writer with Progress Support

4.38/5 (8 votes)
21 Apr 2009CPOL2 min read 39.1K   818  
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)