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

屬性值

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);
}
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 中, IsAsync true 表示已針對重迭的 i/o 開啟控制碼,因此需要和的不同參數 ReadFile WriteFile

當您 FileStream 使用具有 isAsyncuseAsync 或參數的函式建立類別的實例時,請指定這個值 options 。 當屬性為時 true ,資料流程會利用重迭的 i/o 來以非同步方式執行檔案作業。 但是, IsAsync 屬性不一定要 true 呼叫 ReadAsyncWriteAsyncCopyToAsync 方法。 當 IsAsync 屬性為 false 且您呼叫非同步讀取和寫入作業時,UI 執行緒仍不會被封鎖,但實際的 i/o 作業會以同步方式執行。

適用於

另請參閱