FileStream.IsAsync Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient une valeur qui indique si le handle sous-jacent est configuré pour les E/S asynchrones.
public:
virtual property bool IsAsync { bool get(); };
public virtual bool IsAsync { get; }
member this.IsAsync : bool
Public Overridable ReadOnly Property IsAsync As Boolean
Valeur de propriété
truesi le handle a été ouvert pour les E/S superposées sur Windows. Sur Unix, true si le flux a été ouvert pour les E/S asynchrones dans .NET 10 et versions antérieures ou si le descripteur de fichier a l’indicateur O_NONBLOCK défini dans .NET 11 et versions ultérieures ; sinon, false.
Exemples
Cet exemple de code fait partie d’un exemple plus large fourni pour le FileStream(String, FileMode, FileAccess, FileShare, Int32, Boolean) constructeur.
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
Remarques
La IsAsync propriété indique si le FileStream handle est configuré pour les E/S asynchrones, ce qui permet à votre code d’utiliser la Handle propriété correctement. Dans Win32, IsAsync étant vrai signifie que le handle a été ouvert pour les E/S superposées, et nécessite donc différents paramètres à ReadFile et WriteFile. Sur Unix, à partir de .NET 11, il indique si le descripteur de fichier a le O_NONBLOCK jeu d’indicateurs.
Vous spécifiez cette valeur lorsque vous créez une instance de la classe à l’aide d’un constructeur FileStream qui a un isAsync, useAsync, ou options un paramètre. La propriété n’a pas besoin d’appeler true la méthode , WriteAsyncou CopyToAsync la ReadAsyncméthode. Lorsque la propriété est false et que vous appelez une méthode de lecture ou d’écriture asynchrone, le thread d’interface utilisateur n’est pas bloqué, mais l’E/S est effectuée de manière synchrone.