Async.RunSynchronously<'T>-Methode (F#)
Führt die bereitgestellte asynchrone Berechnung aus und wartet auf das Ergebnis.
Namespace/Modulpfad: Microsoft.FSharp.Control
Assembly: FSharp.Core (in FSharp.Core.dll)
// Signature:
static member RunSynchronously : Async<'T> * ?int * ?CancellationToken -> 'T
// Usage:
Async.RunSynchronously (computation)
Async.RunSynchronously (computation, timeout = timeout, cancellationToken = cancellationToken)
Parameter
computation
Typ: Async<'T>Die Berechnung, die ausgeführt werden soll.
timeout
Typ: intDie Zeitdauer in Millisekunden, für die auf das Ergebnis der Berechnung gewartet werden soll, bevor eine TimeoutException ausgelöst wird. Wird kein Timeoutwert angegeben, wird der Standardwert -1 entsprechend Infinite verwendet.
cancellationToken
Typ: CancellationTokenDas Abbruchtoken, das der Berechnung zugeordnet werden soll. Wenn kein Token angegeben wird, wird das Standardabbruchtoken verwendet.
Rückgabewert
Das Ergebnis der Berechnung.
Hinweise
Wenn bei der asynchronen Berechnung eine Ausnahme auftritt, wird erneut eine Ausnahme von dieser Funktion ausgelöst. Wenn kein Abbruchtoken bereitgestellt wird, wird das Standardabbruchtoken verwendet. Der Timeoutparameter wird in Millisekunden angegeben. Der Wert -1 entspricht Infinite.
Wenn Sie ein abbrechbares Abbruchtoken bereitstellen, wird das Timeout ignoriert. Stattdessen können Sie ein eigenes Timeout implementieren, indem Sie den Vorgang abbrechen. Ein Abbruchtoken ist abbrechbar, wenn die CanBeCanceled-Eigenschaft auf true festgelegt ist.
Async.RunSynchronously sollte nicht für den Hauptthread in asynchronen Programmierumgebungen, z. B. in Silverlight-basierten Anwendungen, verwendet werden.
Beispiel
Im folgenden Beispiel wird gezeigt, wie mit Async.RunSynchronously eine asynchrone Berechnung, die mithilfe von Async.Parallel erstellt wurde, ohne Timeout ausgeführt wird.
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
Im folgenden Beispiel wird gezeigt, wie Async.RunSynchronously mit einem Timeout verwendet wird.
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
Beispielausgabe
Plattformen
Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2
Versionsinformationen
F#-Kernbibliotheksversionen
Unterstützt in: 2.0, 4.0, Portable