System.IO.FileStream.BeginWrite 方法

方法描述

开始异步写。

语法定义(C# System.IO.FileStream.BeginWrite 方法 的用法)

[HostProtectionAttribute(SecurityAction.LinkDemand, ExternalThreading = true)]
public override IAsyncResult BeginWrite(
	byte[] array,
	int offset,
	int numBytes,
	AsyncCallback userCallback,
	Object stateObject
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
array System-Byte[] 包含要写入当前流的数据的缓冲区。
offset System-Int32 array 中的从零开始的字节偏移量,从此处开始将字节复制到当前流。
numBytes System-Int32 最多写入的字节数。
userCallback System-AsyncCallback 异步写操作完成后调用的方法。
stateObject System-Object 一个用户提供的对象,它将该特定的异步写入请求与其他请求区别开来。
返回值 System.IAsyncResult 引用异步写入的对象。

提示和注释

对于 BeginWrite 中的每个 IAsyncResult,必须正好调用一次 EndWrite。 EndWrite 将一直被阻止到 I/O 操作完成时。

此方法重写 BeginWrite。

FileStream 提供了两种不同的操作模式:同步 I/O 和异步 I/O。 尽管两者都可使用,但是基础操作系统资源可能只允许采用其中一种模式进行访问。 默认情况下,FileStream 同步打开操作系统句柄。 在 Windows 中,这将降低异步方法的速度。 如果使用异步方法,请使用 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 构造函数。

如果流已关闭或者传递了无效的参数,将立即从 BeginWrite 中引发异常。 在异步写入请求过程中出现的错误(例如 IO 请求过程中的磁盘失败)会在线程池线程上发生,并且在调用 EndWrite 变为可见。

多个同时进行的异步请求会使请求完成顺序不确定。

有关通用 I/O 任务的列表,请参见通用 I/O 任务。

注意

应用到此类型或成员的 HostProtectionAttribute 特性具有以下 Resources 属性值:ExternalThreading。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标、键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护特性。

System.IO.FileStream.BeginWrite 方法例子

此代码示例是为 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 构造函数提供的一个更大示例的一部分。

static void Main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    byte[] writeArray = new byte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream = 
        new FileStream("Test#@@#.dat", FileMode.Create, 
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        fStream.IsAsync ? "" : "not ");

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(
        writeArray, 0, writeArray.Length, 
        new AsyncCallback(EndWriteCallback), 
        new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait 
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
}

异常

异常 异常描述
ArgumentException array 长度减去 offset 小于 numBytes。
ArgumentNullException array 为 null。
ArgumentOutOfRangeException offset 或 numBytes 为负。
NotSupportedException 流不支持写入。
ObjectDisposedException 流已关闭。
IOException 发生了 I/O 错误。

命名空间

namespace: System.IO

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。