FileStream.IsAsync FileStream.IsAsync FileStream.IsAsync FileStream.IsAsync Property

定义

获取一个值,它指示 FileStream 是异步打开还是同步打开的。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

属性值

如果 FileStream 是异步打开的,则为 true,否则为 falsetrue if the FileStream was opened asynchronously; otherwise, false.

示例

此代码示例是为FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean)构造函数提供的更大示例的一部分。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

注解

属性检测FileStream句柄是否已异步打开,使代码可以正确使用Handle属性。 IsAsyncThe IsAsync property detects whether the FileStream handle was opened asynchronously, enabling your code to use the Handle property correctly. 在 Win32 中IsAsync ,为 true 表示为重叠 i/o 打开了句柄,因此需要将不同的参数用于ReadFileWriteFileIn Win32, IsAsync being true means the handle was opened for overlapped I/O, and thus requires different parameters to ReadFile and WriteFile.

使用FileStream 具有useAsync、或参数options的构造函数创建类的实例时,可以指定此值。 isAsyncYou specify this value when you create an instance of the FileStream class using a constructor that has an isAsync, useAsync, or options parameter. 当属性为true时,流将使用重叠的 i/o 以异步方式执行文件操作。When the property is true, the stream utilizes overlapped I/O to perform file operations asynchronously. 但是, IsAsync属性true不必调用ReadAsyncWriteAsync或方法。CopyToAsyncHowever, the IsAsync property does not have to be true to call the ReadAsync, WriteAsync, or CopyToAsync method. 当属性为false ,并且您调用异步读写操作时,UI 线程仍不会被阻止,但实际的 i/o 操作将以同步方式执行。 IsAsyncWhen 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.

适用于

另请参阅