다음을 통해 공유


Async.RunSynchronously<'T> 메서드(F#)

제공된 비동기 계산을 실행하고 그 결과를 기다립니다.

네임스페이스/모듈 경로: Microsoft.FSharp.Control

어셈블리: FSharp.Core(FSharp.Core.dll)

// Signature:
static member RunSynchronously : Async<'T> * ?int * ?CancellationToken -> 'T

// Usage:
Async.RunSynchronously (computation)
Async.RunSynchronously (computation, timeout = timeout, cancellationToken = cancellationToken)

매개 변수

  • computation
    형식: Async<'T>

    실행할 계산입니다.

  • timeout
    형식: int

    TimeoutException이 발생하기 전에 계산 결과를 기다릴 시간(밀리초)입니다. 제한 시간 값이 지정되지 않으면 Infinite에 해당하도록 -1의 기본값이 사용됩니다.

  • cancellationToken
    형식: CancellationToken

    계산과 연결할 취소 토큰입니다. 취소 토큰을 지정하지 않으면 기본 취소 토큰이 사용됩니다.

반환 값

계산 결과입니다.

설명

비동기 계산에서 예외가 발생하면 이 함수에 의해 예외가 다시 발생합니다. 취소 토큰을 지정하지 않으면 기본 취소 토큰이 사용됩니다. 제한 시간 매개 변수는 밀리초 단위로 제공됩니다. -1 값은 Infinite와 같습니다.

취소할 수 있는 취소 토큰을 제공 하는 경우, 시간 제한이 무시 됩니다. 대신, 작업을 취소하여 자신의 시간을 구현할 수 있습니다. 만약 CanBeCanceled 속성이 true로 설정되면 취소할 수 있는 토큰은 취소될 수 있습니다.

Async.RunSynchronously는 Silverlight 기반 응용 프로그램 같은 비동기 프로그래밍 환경에서 주 스레드에 사용해서는 안 됩니다.

예제

다음 예제에서는 Async.RunSynchronously를 사용하여 Async.Parallel을 통해 생성된 비동기 계산을 실행하는 방법을 어떠한 시간 제한 없이 보여줍니다.

let bufferData (number:int) =
    [| for count in 1 .. 1000 -> byte (count % 256) |]
    |> Array.permute (fun index -> index)

let writeFile fileName bufferData =
    async {
      use outputFile = System.IO.File.Create(fileName)
      do! outputFile.AsyncWrite(bufferData) 
    }

Seq.init 1000 (fun num -> bufferData num)
|> Seq.mapi (fun num value -> writeFile ("file" + num.ToString() + ".dat") value)
|> Async.Parallel
|> Async.RunSynchronously
|> ignore

다음 예제에서는 시간 제한과 함께 Async.RunSynchronously를 사용하는 방법을 보여 줍니다.

let bufferData (number:int) =
    [| for i in 1 .. 1000 -> byte (i % 256) |]
    |> Array.permute (fun index -> index)

// Create a counter as a reference cell that can be modified in parallel. 
let counter = ref 0

// writeFileInner writes the data to an open stream 
// that represents the file. It also updates the counter. 

// The counter is locked because it will be accessed by 
// multiple asynchronous computations. 

// The counter must be updated as soon as the 
// AsyncWrite completes, in the same synchronous 
// program flow. There must not be a let! or do! between 
// the AsyncWrite call and the counter update. 
let writeFileInner (stream:System.IO.Stream) data =
    let result = stream.AsyncWrite(data)
    lock counter (fun () -> counter := !counter + 1)
    result

// writeFile encapsulates the asynchronous write operation. 
// The do! includes both the file I/O operation and the 
// counter update in order to keep those operations 
// together. 
let writeFile fileName bufferData =
    async {
      use outputFile = System.IO.File.Create(fileName)
      do! writeFileInner outputFile bufferData
      // Updating the counter here would not be effective.
    }

let async1 = Seq.init 1000 (fun num -> bufferData num)
             |> Seq.mapi (fun num value ->
                 writeFile ("file" + num.ToString() + ".dat") value)
             |> Async.Parallel
try
    Async.RunSynchronously(async1, 100) |> ignore
with
   | exc -> printfn "%s" exc.Message
            printfn "%d write operations completed successfully." !counter

샘플 출력

  

플랫폼

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

버전 정보

F# Core 라이브러리 버전

2.0, 4.0, 노트북 지원

참고 항목

참조

Control.Async 클래스(F#)

Microsoft.FSharp.Control 네임스페이스(F#)