FileStream.IsAsync 属性

定义

获取一个值,它指示 FileStream 是异步打开还是同步打开的。

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,否则为 false

示例

此代码示例是为构造函数提供的更大示例的 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 一部分。

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);
}
// Create a synchronization object that gets
// signaled when verification is complete.
let manualEvent = new ManualResetEvent false

// Create random data to write to the file.
let writeArray = Array.zeroCreate 100000
Random.Shared.NextBytes writeArray

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

// Check that the FileStream was opened asynchronously.

if fStream.IsAsync then "" else "not "
|> printfn "fStream was %sopened asynchronously."

// Asynchronously write to the file.
let asyncResult =
    fStream.BeginWrite(
        writeArray,
        0,
        writeArray.Length,
        AsyncCallback endWriteCallback,
        State(fStream, writeArray, manualEvent)
    )

// Concurrently do other work and then wait
// for the data to be written and verified.
manualEvent.WaitOne(5000, false) |> ignore
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

注解

属性 IsAsync 检测句柄是否已 FileStream 异步打开,使代码能够正确使用 Handle 属性。 在 Win32 中,如果为 true,IsAsync则表示为重叠 I/O 打开句柄,因此需要对 和 WriteFile使用不同的参数ReadFile

使用具有 isAsync、 或 options 参数的构造函数创建 类的FileStream实例时,useAsync可以指定此值。 当 属性为 true时,流利用重叠的 I/O 异步执行文件操作。 但是, IsAsync 属性不必是 true 调用 ReadAsyncWriteAsyncCopyToAsync 方法。 IsAsync当 属性为 false 并且调用异步读取和写入操作时,UI 线程仍不会被阻止,但实际 I/O 操作将同步执行。

适用于

另请参阅