Share via


FileStream.EndRead(IAsyncResult) Metode

Definisi

Menunggu operasi baca asinkron yang tertunda selesai. (Pertimbangkan untuk menggunakan ReadAsync(Byte[], Int32, Int32, CancellationToken) sebagai gantinya.)

public:
 override int EndRead(IAsyncResult ^ asyncResult);
public override int EndRead (IAsyncResult asyncResult);
override this.EndRead : IAsyncResult -> int
Public Overrides Function EndRead (asyncResult As IAsyncResult) As Integer

Parameter

asyncResult
IAsyncResult

Referensi ke permintaan asinkron yang tertunda untuk menunggu.

Mengembalikan

Jumlah byte yang dibaca dari aliran, antara 0 dan jumlah byte yang Anda minta. Stream hanya mengembalikan 0 di akhir aliran, jika tidak, streaming harus diblokir hingga setidaknya 1 byte tersedia.

Pengecualian

asyncResultadalah null.

Objek ini IAsyncResult tidak dibuat dengan memanggil BeginRead(Byte[], Int32, Int32, AsyncCallback, Object) pada kelas ini.

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 EndReadCallback( IAsyncResult^ asyncResult )
   {
      State^ tempState = dynamic_cast<State^>(asyncResult->AsyncState);
      int readCount = tempState->FStream->EndRead( asyncResult );
      int i = 0;
      while ( i < readCount )
      {
         if ( tempState->ReadArray[ i ] != tempState->WriteArray[ i++ ] )
         {
            Console::WriteLine( "Error writing data." );
            tempState->FStream->Close();
            return;
         }
      }

      Console::WriteLine( "The data was written to {0} "
      "and verified.", tempState->FStream->Name );
      tempState->FStream->Close();
      
      // Signal the main thread that the verification is finished.
      tempState->ManualEvent->Set();
   }


public:
static void EndReadCallback(IAsyncResult asyncResult)
{
    State tempState = (State)asyncResult.AsyncState;
    int readCount = tempState.FStream.EndRead(asyncResult);

    int i = 0;
    while(i < readCount)
    {
        if(tempState.ReadArray[i] != tempState.WriteArray[i++])
        {
            Console.WriteLine("Error writing data.");
            tempState.FStream.Close();
            return;
        }
    }
    Console.WriteLine("The data was written to {0} and verified.",
        tempState.FStream.Name);
    tempState.FStream.Close();

    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set();
}
let endReadCallback (asyncResult: IAsyncResult) =
    let tempState = asyncResult.AsyncState :?> State
    let readCount = tempState.FStream.EndRead asyncResult

    let mutable i = 0
    let mutable errored = false

    while i < readCount do
        if tempState.ReadArray[i] <> tempState.WriteArray[i] then
            printfn "Error writing data."
            tempState.FStream.Close()
            errored <- true
            i <- readCount

        i <- i + 1

    printfn $"The data was written to {tempState.FStream.Name} and verified."
    tempState.FStream.Close()
    // Signal the main thread that the verification is finished.
    tempState.ManualEvent.Set() |> ignore
Private Shared Sub EndReadCallback(asyncResult As IAsyncResult)
     Dim tempState As State = _
         DirectCast(asyncResult.AsyncState, State)
     Dim readCount As Integer = _
         tempState.FStream.EndRead(asyncResult)

     Dim i As Integer = 0
     While(i < readCount)
         If(tempState.ReadArray(i) <> tempState.WriteArray(i))
             Console.WriteLine("Error writing data.")
             tempState.FStream.Close()
             Return
         End If
         i += 1
     End While

     Console.WriteLine("The data was written to {0} and " & _
         "verified.", tempState.FStream.Name)
     tempState.FStream.Close()

     ' Signal the main thread that the verification is finished.
     tempState.ManualEvent.Set()
 End Sub

Keterangan

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

EndRead harus dipanggil persis untuk setiap panggilan ke BeginRead. Gagal mengakhiri proses baca sebelum memulai bacaan lain dapat menyebabkan perilaku yang tidak diinginkan seperti kebuntuan.

Metode ini mengambil EndReadalih .

EndRead dapat dipanggil pada setiap IAsyncResult dari BeginRead. EndRead Panggilan memberi tahu Anda berapa banyak byte yang dibaca dari aliran. EndRead akan memblokir hingga operasi I/O selesai.

Berlaku untuk

Lihat juga