Condividi tramite


Metodo Async.RunSynchronously<'T> (F#)

Esegue il calcolo asincrono e attende il relativo risultato.

Percorso di spazio dei nomi/modulo: 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)

Parametri

  • computation
    Tipo: Async<'T>

    Calcolo da eseguire.

  • timeout
    Tipo: int

    Quantità di tempo, in millisecondi, di attesa del risultato del calcolo prima di generare un'eccezione TimeoutException Se per il timeout non viene fornito alcun valore, viene utilizzato un valore predefinito -1 in modo che corrisponde a Infinite.

  • cancellationToken
    Tipo: CancellationToken

    Token di annullamento da associare al calcolo. Se non ne viene fornito alcuno, viene utilizzato il token di annullamento predefinito.

Valore restituito

Risultato del calcolo.

Note

Se durante il calcolo asincrono si verifica un'eccezione, viene nuovamente generata un'eccezione da questa funzione. Se non viene specificato alcun token di annullamento, viene utilizzato il token di annullamento predefinito. Il parametro di timeout viene specificato in millisecondi. Valore di -1 equivale a Infinite.

Async.RunSynchronously non deve essere utilizzato nel thread principale in ambienti di programmazione asincrona, ad esempio in applicazioni basate su Silverlight.

Esempio

Nell'esempio riportato di seguito viene illustrato come utilizzare Async.RunSynchronously per eseguire un calcolo asincrono creato tramite Async.Parallel, senza timeout.

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

Nell'esempio riportato di seguito viene illustrato come utilizzare Async.RunSynchronously con un timeout.

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

Output di esempio

      

Piattaforme

Windows 7, Windows Vista SP2, Windows XP SP3, Windows XP x64 SP2, Windows Server 2008 R2, Windows Server 2008 SP2, Windows Server 2003 SP2.

Informazioni sulla versione

F# Runtime

Supportato in: 2.0, 4.0

Silverlight

Supportato in: 3

Vedere anche

Riferimenti

Classe Control.Async (F#)

Spazio dei nomi Microsoft.FSharp.Control (F#)

Cronologia delle modifiche

Data

Cronologia

Motivo

Luglio 2010

Aggiunto esempi di codice.

Miglioramento delle informazioni.