다음을 통해 공유


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

속성 값

true 이 비동기적으로 열렸으면 FileStream 이고, 그렇지 않으면 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);
}
// 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 비동기적으로 열렸는지 여부를 검색하여 코드에서 Handle 속성을 올바르게 사용할 수 있도록 합니다. Win32 IsAsync 에서 true이면 겹치는 I/O에 대해 핸들이 열렸으므로 및 WriteFile에 대한 다른 매개 변수가 ReadFile 필요합니다.

, useAsync또는 options 매개 변수가 있는 생성자를 isAsync사용하여 클래스의 FileStream instance 만들 때 이 값을 지정합니다. 속성이 이면 true스트림은 겹치는 I/O를 사용하여 파일 작업을 비동기적으로 수행합니다. 그러나 속성은 IsAsync , WriteAsync또는 CopyToAsync 메서드를 호출ReadAsynctrue 필요가 없습니다. 속성이 IsAsyncfalse 고 비동기 읽기 및 쓰기 작업을 호출하는 경우 UI 스레드는 여전히 차단되지 않지만 실제 I/O 작업은 동기적으로 수행됩니다.

적용 대상

추가 정보