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 では、true を指定すると、IsAsync重複する I/O に対してハンドルが開かれたため、異なるパラメーターを必要とWriteFileしますReadFile

この値は、、またはパラメーターを持つコンストラクターをFileStream使用してクラスのインスタンスをisAsync``useAsync``options作成するときに指定します。 プロパティが指定されている場合、ストリームは true重複した I/O を使用してファイル操作を非同期的に実行します。 ただし、プロパティで IsAsyncWriteAsyncまたはCopyToAsyncメソッドを呼び出すReadAsync必要trueはありません。 プロパティがIsAsyncfalse非同期の読み取りおよび書き込み操作を呼び出しても、UI スレッドはブロックされませんが、実際の I/O 操作は同期的に実行されます。

適用対象

こちらもご覧ください