Compartir a través de


FileStream.EndWrite(IAsyncResult) Método

Definición

Termina una operación de escritura asincrónica y se bloquea hasta que se completa la operación de E/S. (Considere usar WriteAsync(Byte[], Int32, Int32, CancellationToken) en su lugar).

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

Parámetros

asyncResult
IAsyncResult

La solicitud de E/S asincrónica pendiente.

Excepciones

asyncResult es null.

Este objeto IAsyncResult no se creó mediante una llamada a BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object) en esta clase.

La secuencia está cerrada o se produjo un error interno.

Ejemplos

Este ejemplo de código forma parte de un ejemplo más grande proporcionado para el FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

   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

Comentarios

En .NET Framework 4 y versiones anteriores, tiene que usar métodos como BeginWrite e EndWrite implementar operaciones de archivo asincrónicas. Estos métodos siguen estando disponibles en .NET Framework 4.5 para admitir código heredado; sin embargo, los nuevos métodos asincrónicos, como ReadAsync, WriteAsync, CopyToAsyncy FlushAsync, le ayudan a implementar operaciones de archivos asincrónicas más fácilmente.

Este método invalida EndWrite.

EndWrite se debe llamar exactamente una vez en cada IAsyncResult desde BeginWrite. EndWrite se bloqueará hasta que se haya completado la operación de E/S.

Se aplica a

Consulte también