FileStream.IsAsync 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得一個值,表示底層的句柄是否被設定為非同步 I/O。
public:
virtual property bool IsAsync { bool get(); };
public virtual bool IsAsync { get; }
member this.IsAsync : bool
Public Overridable ReadOnly Property IsAsync As Boolean
屬性值
true如果在 Windows 上為重疊 I/O 開啟了這個柄。 在 Unix 上,true如果串流在 .NET 10 及更早版本中開啟非同步 I/O,或檔案描述符在 .NET 11 及以後版本設定O_NONBLOCK了標誌;否則, false
範例
此程式碼範例是建構子更大範例 FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) 的一部分。
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 句柄是否被設定為非同步 I/O,讓你的程式碼能正確使用這個 Handle 屬性。 在 Win32 中, IsAsync 為真表示 handle 是為了重疊 I/O而開啟,因此需要不同的參數 和 ReadFileWriteFile。 在 Unix 上,從 .NET 11 開始,會反映檔案描述符是否設定了O_NONBLOCK該標誌。
當您使用具有FileStream、 isAsync或 useAsync 參數的建構函式來建立 類別的options實例時,您可以指定此值。 該屬性不必是 , true 才能呼叫 ReadAsync、 WriteAsync或 CopyToAsync 方法。 當屬性是 且 false 你呼叫非同步讀寫方法時,UI 執行緒不會被阻塞,但輸入輸出是同步執行的。