FileStream.IsAsync 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,指出 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 中,IsAsync
為 true 表示已針對重疊 I/O 開啟句柄,因此需要 和 WriteFile
的不同參數ReadFile
。
當您使用具有isAsync
、 useAsync
或 options
參數的建構函式建立 類別的FileStream實例時,可以指定這個值。 當 屬性為 true
時,數據流會利用重疊的 I/O 以異步方式執行檔案作業。 不過, IsAsync 屬性不需要 true
呼叫 ReadAsync、 WriteAsync或 CopyToAsync 方法。 IsAsync當 屬性是 false
且您呼叫異步讀取和寫入作業時,UI 線程仍然不會遭到封鎖,但實際的 I/O 作業會以同步方式執行。