Bagikan melalui


FileStream.EndWrite(IAsyncResult) Metode

Definisi

Mengakhiri operasi tulis asinkron dan memblokir hingga operasi I/O selesai. (Pertimbangkan untuk menggunakan WriteAsync(Byte[], Int32, Int32, CancellationToken) sebagai gantinya.)

public:
 override void EndWrite(IAsyncResult ^ asyncResult);
public override void EndWrite (IAsyncResult asyncResult);
override this.EndWrite : IAsyncResult -> unit
Public Overrides Sub EndWrite (asyncResult As IAsyncResult)

Parameter

asyncResult
IAsyncResult

Permintaan I/O asinkron yang tertunda.

Pengecualian

asyncResultadalah null.

Aliran ditutup atau terjadi kesalahan internal.

Contoh

Contoh kode ini adalah bagian dari contoh yang lebih besar yang disediakan untuk FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) konstruktor.

   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

Keterangan

Dalam .NET Framework 4 dan versi yang lebih lama, Anda harus menggunakan metode seperti BeginWrite dan EndWrite untuk menerapkan operasi file asinkron. Metode ini masih tersedia dalam .NET Framework 4.5 untuk mendukung kode warisan; namun, metode asinkron baru, seperti ReadAsync, WriteAsync, CopyToAsync, dan FlushAsync, membantu Anda menerapkan operasi file asinkron dengan lebih mudah.

Metode ini mengambil alih EndWrite.

EndWrite harus dipanggil tepat sekali pada setiap IAsyncResult dari BeginWrite. EndWrite akan memblokir hingga operasi I/O selesai.

Berlaku untuk

Lihat juga