Click here to Skip to main content
Click here to Skip to main content

Asynchronous Stream Reader and Writer with Progress Support

, 21 Apr 2009
Rate this:
Please Sign up or sign in to vote.
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:

/// <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:

/// <summary>
/// Initializes a new empty instance of the AsyncStreaming.AsyncStreamWriter class.
/// </summary>
public AsyncStreamWriter()
/// <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) 
/// <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) 
/// <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)
/// <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)

Share

About the Author

mbostjan
Web Developer
Slovenia Slovenia
No Biography provided

Comments and Discussions

 
QuestionConvert to VS 2005? PinmemberMember 12062791-Oct-09 18:21 
GeneralArticle Pinmemberstixoffire27-Apr-09 17:50 
GeneralNice little program not so good article PinmemberPaw Jershauge21-Apr-09 21:28 
GeneralRe: Nice little program not so good article Pinmembermbostjan23-Apr-09 7:35 
GeneralRe: Nice little program not so good article PinmemberPaw Jershauge23-Apr-09 20:24 

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.

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 21 Apr 2009
Article Copyright 2009 by mbostjan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid