Async.RunSynchronously<'T> メソッド (F#)

更新 : 2010 年 7 月

非同期計算を実行し、その結果を待機します。

名前空間/モジュール パス: 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 と同じです。

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 7、Windows Vista SP2、Windows XP SP3、Windows XP x64 SP2、Windows Server 2008 R2、Windows Server 2008 SP2、Windows Server 2003 SP2

バージョン情報

F# ランタイム

サポート対象: 2.0、4.0

Silverlight

サポート: 3

参照

その他の技術情報

Control.Async クラス (F#)

Microsoft.FSharp.Control 名前空間 (F#)

履歴の変更

日付

履歴

理由

2010 年 7 月

コード例を追加。

情報の拡充