共用方式為


FileStream.EndWrite(IAsyncResult) 方法

定義

結束非同步寫入作業並封鎖,直到 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 要求。

例外狀況

asyncResultnull

這個 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 和舊版中,您必須使用 和 之類的BeginWriteEndWrite方法來實作異步檔案作業。 這些方法仍可在 .NET Framework 4.5 中使用,以支援舊版程式代碼;不過,新的異步方法,例如 ReadAsyncWriteAsyncCopyToAsyncFlushAsync,可協助您更輕鬆地實作異步檔案作業。

這個方法會覆寫 EndWrite

EndWrite每個都必須從BeginWrite呼叫一次IAsyncResultEndWrite 將會封鎖直到 I/O 作業完成為止。

適用於

另請參閱