65.9K
CodeProject 正在变化。 阅读更多。
Home

异步流读取器和写入器(带进度支持)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (8投票s)

2009年4月21日

CPOL

2分钟阅读

viewsIcon

39485

downloadIcon

834

AsyncStreaming

AsyncStreaming - Click to enlarge image

引言

AsyncStreaming 是一个用于异步流读取和写入的类库。 AsyncStreaming 支持读取和写入的进度报告。

结构

使用 AsynStreamReaderAsyncStreamWriter 的常用类

  • AsyncStreamException - 此类表示异步流处理期间发生的错误。
  • AsyncStreamStateChangeArgs - 此类为状态更改事件提供数据。
  • AsyncStreamErrorEventArgs - 此类为错误事件提供数据。
  • AsyncStreamState - 此枚举指定用于指示 AsyncStreaming 状态的标识符。

可能的状态

  • 就绪 - 当流准备好启动时发生此状态。
  • 已启动 - 当流已启动时发生此状态。
  • 已暂停 - 当流已暂停时发生此状态。
  • 已停止 - 当流已停止时发生此状态。
  • 已完成 - 当流已完成时发生此状态。
  • 错误 - 当发生流异常时发生此状态。

AsyncStreamReader

read_progress.jpg - Click to enlarge image
  • BaseAsyncStreamReader - 异步读取的基本功能。因此,您可以轻松使用此类在您自己的读取类中实现基本功能。
  • AsyncStreamReader - 从 BaseAsyncStreamReader 类扩展并添加构造函数的类。

Public 方法

  • StartRead() - 启动异步读取操作
  • PauseRead() – 暂停异步读取操作
  • ResumeRead() – 恢复已暂停的异步读取操作
  • StopRead() – 停止异步读取操作

事件

  • OnReadedBytes – 当进度增加百分之一时发生
  • OnEndRead – 当所有字节都被读取时发生
  • OnError – 当发生 AsyncStreamExcpetion 时发生
  • OnStateChanged – 当 AsyncStreamReader 的状态改变时发生

构造函数

/// <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 – 异步写入的基本功能。因此,您可以轻松使用此类在您自己的写入类中实现基本功能。
  • AsyncStreamWriter – 从 BaseAsyncStreamWriter 类扩展并添加构造函数的类。

Public 方法

  • StartWrite() – 开始异步写入操作
  • PauseWrite() – 暂停异步写入操作
  • ResumeWrite() – 恢复已暂停的异步写入操作
  • StopWrite() – 停止异步写入操作

事件

  • OnStateChanged – 当 AsyncStreamWriter 的状态改变时发生
  • OnWritedBytes – 当进度增加百分之一时发生
  • OnEndWrite – 当所有字节都被写入时发生
  • OnError - 当发生 AsyncException 时发生

构造函数

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

历史

  • 2009 年 4 月 21 日:初始发布
© . All rights reserved.