FileStream.EndWrite(IAsyncResult) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
结束异步写入操作,在 I/O 操作完成之前一直阻止。 (请考虑改用 WriteAsync(Byte[], Int32, Int32, CancellationToken)。)
public:
override void EndWrite(IAsyncResult ^ asyncResult);
public override void EndWrite (IAsyncResult asyncResult);
override this.EndWrite : IAsyncResult -> unit
Public Overrides Sub EndWrite (asyncResult As IAsyncResult)
参数
- asyncResult
- IAsyncResult
挂起的异步 I/O 请求。
例外
asyncResult
为 null
。
此 IAsyncResult 对象不是通过对该类调用 BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) 来创建的。
流已关闭或出现了内部错误。
示例
此代码示例是为构造函数提供的更大示例的 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 一部分。
static void EndWriteCallback( IAsyncResult^ asyncResult )
{
State^ tempState = dynamic_cast<State^>(asyncResult->AsyncState);
FileStream^ fStream = tempState->FStream;
fStream->EndWrite( asyncResult );
// Asynchronously read back the written data.
fStream->Position = 0;
asyncResult = fStream->BeginRead( tempState->ReadArray, 0, tempState->ReadArray->Length, gcnew AsyncCallback( &FStream::EndReadCallback ), tempState );
// Concurrently do other work, such as
// logging the write operation.
}
};
static void EndWriteCallback(IAsyncResult asyncResult)
{
State tempState = (State)asyncResult.AsyncState;
FileStream fStream = tempState.FStream;
fStream.EndWrite(asyncResult);
// Asynchronously read back the written data.
fStream.Position = 0;
asyncResult = fStream.BeginRead(
tempState.ReadArray, 0 , tempState.ReadArray.Length,
new AsyncCallback(EndReadCallback), tempState);
// Concurrently do other work, such as
// logging the write operation.
}
let endWriteCallback (asyncResult: IAsyncResult) =
let tempState = asyncResult.AsyncState :?> State
let fStream = tempState.FStream
fStream.EndWrite asyncResult
// Asynchronously read back the written data.
fStream.Position <- 0
let asyncResult =
fStream.BeginRead(tempState.ReadArray, 0, tempState.ReadArray.Length, AsyncCallback endReadCallback, tempState)
// Concurrently do other work, such as
// logging the write operation.
()
Private Shared Sub EndWriteCallback(asyncResult As IAsyncResult)
Dim tempState As State = _
DirectCast(asyncResult.AsyncState, State)
Dim fStream As FileStream = tempState.FStream
fStream.EndWrite(asyncResult)
' Asynchronously read back the written data.
fStream.Position = 0
asyncResult = fStream.BeginRead( _
tempState.ReadArray, 0 , tempState.ReadArray.Length, _
AddressOf EndReadCallback, tempState)
' Concurrently do other work, such as
' logging the write operation.
End Sub
注解
在 .NET Framework 4 及更早版本中,必须使用 和 EndWrite 等BeginWrite方法来实现异步文件操作。 这些方法在 .NET Framework 4.5 中仍然可用,以支持旧代码;但是,新的异步方法(如 ReadAsync、WriteAsync、 CopyToAsync和 FlushAsync)可帮助你更轻松地实现异步文件操作。
此方法重写 EndWrite。
EndWrite 必须对 中的每个 IAsyncResult 调用 BeginWrite一次。 EndWrite 将阻止,直到 I/O 操作完成。