Condividi tramite


Metodo Async.SwitchToNewThread (F#)

Crea un calcolo asincrono che genera un nuovo thread ed esegue la relativa continuazione in tale thread.

Percorso di spazio dei nomi/modulo: Microsoft.FSharp.Control

Assembly: FSharp.Core (in FSharp.Core.dll)

// Signature:
static member SwitchToNewThread : unit -> Async<unit>

// Usage:
Async.SwitchToNewThread ()

Valore restituito

Calcolo che verrà eseguito su un nuovo thread.

Esempio

Nell'esempio di codice riportato di seguito viene illustrato come utilizzare Async.SwitchToNewThread e Async.SwitchToThreadPool per eseguire il wrapping di una chiamata sincrona a un metodo come metodo asincrono.

open System
open System.IO

let asyncMethod f = 
    async {  
        do! Async.SwitchToNewThread() 
        let result = f() 
        do! Async.SwitchToThreadPool() 
        return result
    } 

let GetFilesAsync(path) = asyncMethod (fun () -> Directory.GetFiles(path))

let listFiles path =
    async {
        let! files = GetFilesAsync(path)
        Array.iter (fun elem -> printfn "%s" elem) files
    }

printfn "Here we go..."
// The output is interleaved, which shows that these are both 
// running simultaneously.
Async.Start(listFiles ".")
Async.Start(listFiles ".")
Console.WriteLine("Press a key to continue...")
Console.ReadLine() |> ignore

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 esempio di codice.

Miglioramento delle informazioni.