Click here to Skip to main content
Licence CPOL
First Posted 21 Apr 2009
Views 14,771
Downloads 281
Bookmarked 35 times

Asynchronous Stream Reader and Writer with Progress Support

By | 21 Apr 2009 | Article
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)

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
QuestionConvert to VS 2005? PinmemberMember 120627918:21 1 Oct '09  
GeneralArticle Pinmemberstixoffire17:50 27 Apr '09  
GeneralNice little program not so good article PinmemberPaw Jershauge21:28 21 Apr '09  
GeneralRe: Nice little program not so good article Pinmembermbostjan7:35 23 Apr '09  
GeneralRe: Nice little program not so good article PinmemberPaw Jershauge20:24 23 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
Web01 | 2.5.120517.1 | Last Updated 21 Apr 2009
Article Copyright 2009 by mbostjan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid