Condividi tramite


Metodo Async.SwitchToContext (F#)

Consente di creare un calcolo asincrono che esegue la relativa continuazione con il metodo Post sull'oggetto di contesto di sincronizzazione.

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

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

// Signature:
static member SwitchToContext : SynchronizationContext -> Async<unit>

// Usage:
Async.SwitchToContext (syncContext)

Parametri

  • syncContext
    Tipo: SynchronizationContext

    Contesto di sincronizzazione utilizzato per accettare il calcolo inserito.

Valore restituito

Un calcolo asincrono che utilizza il contesto syncContext da eseguire.

Note

Se syncContext è null, il calcolo asincrono equivale a Async.SwitchToThreadPool.

Esempio

Nell'esempio di codice che segue viene illustrato come utilizzare l'oggetto Async.SwitchToContext per passare al thread dell'interfaccia utente per aggiornare l'interfaccia utente. In questo caso, un indicatore di stato che indica lo stato di completamento di un calcolo viene aggiornato.

open System.Windows.Forms

let form = new Form(Text = "Test Form", Width = 400, Height = 400)
let syncContext = System.Threading.SynchronizationContext.Current
let button1 = new Button(Text = "Start")
let label1 = new Label(Text = "", Height = 200, Width = 200,
                       Top = button1.Height + 10)
form.Controls.AddRange([| button1; label1 |] )

type invokeFunction = delegate of unit -> unit

let async1(syncContext, form : System.Windows.Forms.Form) =
    async {
        let label1 = form.Controls.[1]
        // Do something.
        do! Async.Sleep(1000)

        // Switch to the UI thread and update the UI.
        do! Async.SwitchToContext(syncContext)

        let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
        label1.Text <- label1.Text + sprintf "On the UI Thread [%d]\n" threadNumber

        // Switch back to the thread pool.
        do! Async.SwitchToThreadPool()
        // Do something.
        do! Async.Sleep(1000)

        let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId

        // When on a thread pool thread, use Control.Invoke to update UI.
        label1.Invoke(new invokeFunction(fun () -> 
            label1.Text <- label1.Text +
                       sprintf "Switched to thread pool [%d]\n" threadNumber)) |> ignore
    }

let buttonClick(sender:obj, args) =
    let button = sender :?> Button
    Async.Start(async1(syncContext, button.Parent :?> Form))

    let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
    label1.Text <- sprintf "Started asynchronous workflow [%d]\n"  threadNumber
    ()

button1.Click.AddHandler(fun sender args -> buttonClick(sender, args))
Application.Run(form)

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.