FileStream.IsAsync Propriedade

Definição

Obtém um valor que indica se FileStream foi aberto de forma assíncrona ou síncrona.Gets a value that indicates whether the FileStream was opened asynchronously or synchronously.

public:
 virtual property bool IsAsync { bool get(); };
public virtual bool IsAsync { get; }
member this.IsAsync : bool
Public Overridable ReadOnly Property IsAsync As Boolean

Valor da propriedade

Boolean

true se o FileStream tiver sido aberto de forma assíncrona; caso contrário, false.true if the FileStream was opened asynchronously; otherwise, false.

Exemplos

Este exemplo de código é parte de um exemplo maior fornecido para o FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) Construtor.This code example is part of a larger example provided for the FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructor.

int main()
{
   
   // Create a synchronization object that gets 
   // signaled when verification is complete.
   ManualResetEvent^ manualEvent = gcnew ManualResetEvent( false );
   
   // Create the data to write to the file.
   array<Byte>^writeArray = gcnew array<Byte>(100000);
   (gcnew Random)->NextBytes( writeArray );
   FileStream^ fStream = gcnew FileStream(  "Test#@@#.dat",FileMode::Create,FileAccess::ReadWrite,FileShare::None,4096,true );
   
   // Check that the FileStream was opened asynchronously.
   Console::WriteLine( "fStream was {0}opened asynchronously.", fStream->IsAsync ? (String^)"" : "not " );
   
   // Asynchronously write to the file.
   IAsyncResult^ asyncResult = fStream->BeginWrite( writeArray, 0, writeArray->Length, gcnew AsyncCallback( &FStream::EndWriteCallback ), gcnew State( fStream,writeArray,manualEvent ) );
   
   // Concurrently do other work and then wait 
   // for the data to be written and verified.
   manualEvent->WaitOne( 5000, false );
}

static void Main()
{
    // Create a synchronization object that gets
    // signaled when verification is complete.
    ManualResetEvent manualEvent = new ManualResetEvent(false);

    // Create random data to write to the file.
    byte[] writeArray = new byte[100000];
    new Random().NextBytes(writeArray);

    FileStream fStream =
        new FileStream("Test#@@#.dat", FileMode.Create,
        FileAccess.ReadWrite, FileShare.None, 4096, true);

    // Check that the FileStream was opened asynchronously.
    Console.WriteLine("fStream was {0}opened asynchronously.",
        fStream.IsAsync ? "" : "not ");

    // Asynchronously write to the file.
    IAsyncResult asyncResult = fStream.BeginWrite(
        writeArray, 0, writeArray.Length,
        new AsyncCallback(EndWriteCallback),
        new State(fStream, writeArray, manualEvent));

    // Concurrently do other work and then wait
    // for the data to be written and verified.
    manualEvent.WaitOne(5000, false);
}
Shared Sub Main()

    ' Create a synchronization object that gets 
    ' signaled when verification is complete.
    Dim manualEvent As New ManualResetEvent(False)

    ' Create random data to write to the file.
    Dim writeArray(100000) As Byte
    Dim randomGenerator As New Random()
    randomGenerator.NextBytes(writeArray)

    Dim fStream As New FileStream("Test#@@#.dat", _
        FileMode.Create, FileAccess.ReadWrite, _
        FileShare.None, 4096, True)

    ' Check that the FileStream was opened asynchronously.
    If fStream.IsAsync = True
        Console.WriteLine("fStream was opened asynchronously.")
    Else
        Console.WriteLine("fStream was not opened asynchronously.")
    End If

    ' Asynchronously write to the file.
    Dim asyncResult As IAsyncResult = fStream.BeginWrite( _
        writeArray, 0, writeArray.Length, _
        AddressOf EndWriteCallback , _
        New State(fStream, writeArray, manualEvent))

    ' Concurrently do other work and then wait
    ' for the data to be written and verified.
    manualEvent.WaitOne(5000, False)
End Sub

Comentários

A IsAsync Propriedade detecta se o FileStream identificador foi aberto de forma assíncrona, permitindo que seu código use a Handle Propriedade corretamente.The IsAsync property detects whether the FileStream handle was opened asynchronously, enabling your code to use the Handle property correctly. No Win32, IsAsync ser verdadeiro significa que o identificador foi aberto para e/s sobreposta e, portanto, requer parâmetros diferentes para ReadFile e WriteFile .In Win32, IsAsync being true means the handle was opened for overlapped I/O, and thus requires different parameters to ReadFile and WriteFile.

Você especifica esse valor quando cria uma instância da FileStream classe usando um construtor que tem um isAsync useAsync parâmetro, ou options .You specify this value when you create an instance of the FileStream class using a constructor that has an isAsync, useAsync, or options parameter. Quando a propriedade é true , o fluxo utiliza e/s sobreposta para executar operações de arquivo de forma assíncrona.When the property is true, the stream utilizes overlapped I/O to perform file operations asynchronously. No entanto, a IsAsync propriedade não precisa ser true chamar o ReadAsync método, WriteAsync ou CopyToAsync .However, the IsAsync property does not have to be true to call the ReadAsync, WriteAsync, or CopyToAsync method. Quando a IsAsync propriedade é false e você chama as operações de leitura e gravação assíncronas, o thread da interface do usuário ainda não é bloqueado, mas a operação de e/s real é executada de forma síncrona.When the IsAsync property is false and you call the asynchronous read and write operations, the UI thread is still not blocked, but the actual I/O operation is performed synchronously.

Aplica-se a

Confira também