FileStream.IsAsync Proprietà
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene un valore che indica se l'handle sottostante è configurato per l'I/O asincrona.
public:
virtual property bool IsAsync { bool get(); };
public virtual bool IsAsync { get; }
member this.IsAsync : bool
Public Overridable ReadOnly Property IsAsync As Boolean
Valore della proprietà
truese l'handle è stato aperto per operazioni di I/O sovrapposte in Windows. In Unix, true se il flusso è stato aperto per l'I/O asincrono in .NET 10 e versioni precedenti o il descrittore di file ha il O_NONBLOCK flag impostato in .NET 11 e versioni successive; in caso contrario, false.
Esempio
Questo esempio di codice fa parte di un esempio più ampio fornito per il FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) costruttore.
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
Commenti
La IsAsync proprietà indica se l'handle è configurato per l'I FileStream /O asincrona, consentendo al codice di usare correttamente la Handle proprietà . In Win32, IsAsync essendo true significa che l'handle è stato aperto per le operazioni di I/O sovrapposte e quindi richiede parametri diversi per ReadFile e WriteFile. In Unix, a partire da .NET 11, riflette se il descrittore di file ha il O_NONBLOCK flag impostato.
Questo valore viene specificato quando si crea un'istanza della FileStream classe usando un costruttore con un isAsyncparametro , useAsynco options . Non è necessario che la proprietà chiami true il ReadAsyncmetodo , WriteAsynco CopyToAsync . Quando la proprietà è false e si chiama un metodo di lettura o scrittura asincrono, il thread dell'interfaccia utente non viene bloccato, ma l'I/O viene eseguito in modo sincrono.