次の方法で共有


FileStream.IsAsync プロパティ

FileStream が非同期的に開かれたか、同期的に開かれたかを示す値を取得します。

Public Overridable ReadOnly Property IsAsync As Boolean
[C#]
public virtual bool IsAsync {get;}
[C++]
public: __property virtual bool get_IsAsync();
[JScript]
public function get IsAsync() : Boolean;

プロパティ値

FileStream が非同期的に開かれた場合は true 。それ以外の場合は false

解説

IsAsync プロパティは、 FileStream ハンドルが非同期的に開かれたかどうかを検出し、コードで Handle プロパティを正しく使用できるようにします。Win32 では、 IsAsync が TRUE の場合は、重複 I/O 用にハンドルが開かれ、 ReadFileWriteFile で異なるパラメータが要求されます。

Windows 98, Windows Millennium Edition プラットフォームに関する注意点: これらのオペレーティング システムは、非同期 I/O をサポートしていません。これらのプラットフォーム用のコードが非同期的にファイルを開くことを要求する場合にも、共通言語ランタイムは同期的にファイルを開きます。

使用例

[Visual Basic, C#, C++] 次のコード例は System.IO.FileStream.FileStream4 の例の一部です。

 
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

[C#] 
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);
}

[C++] 
void main()
{
    // Create a synchronization object that gets 
    // signaled when verification is complete.
    ManualResetEvent* manualEvent = new ManualResetEvent(false);

    // Create the data to write to the file.
    Byte writeArray __gc[] = new Byte __gc [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(S"fStream was {0}opened asynchronously.",
        fStream->IsAsync ? S"" : S"not ");

    // Asynchronously write to the file.
    IAsyncResult* asyncResult = fStream->BeginWrite(
        writeArray, 0, writeArray->Length, 
        new AsyncCallback(0, &FStream::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);
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

FileStream クラス | FileStream メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み