Async.SwitchToContext — Metoda (F#)

Tworzy asynchroniczne obliczenie, które uruchamia jego kontynuację za pomocą metody Post na obiekcie kontekstu synchronizacji.

Przestrzeń nazw/Ścieżka modułu: Microsoft.FSharp.Control

Zestaw:: FSharp.Core (w FSharp.Core.dll)

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

// Usage:
Async.SwitchToContext (syncContext)

Parametry

Wartość zwracana

Asynchroniczne obliczenie, które używa kontekstu syncContext do wykonania.

Uwagi

Jeśli syncContext ma wartość null, wówczas asynchroniczne obliczenie jest równoważne z Async.SwitchToThreadPool.

Przykład

Poniższy kod ilustruje sposób użycia Async.SwitchToContext aby przełączyć się do wątku interfejsu użytkownika, aby zaktualizować interfejs użytkownika.W tym przypadku, pasek postępu wskazujący stan kompletacji obliczeń jest aktualizowany.

open System.Windows.Forms

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

let async1(syncContext, form : System.Windows.Forms.Form) =
    async {
        let label1 = form.Controls.[1]
        // Do something. 
        do! Async.Sleep(10000)
        let threadName = System.Threading.Thread.CurrentThread.Name
        let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
        label1.Text <- label1.Text + sprintf "Something [%s] [%d]" threadName threadNumber

        // Switch to the UI thread and update the UI. 
        do! Async.SwitchToContext(syncContext)
        let threadName = System.Threading.Thread.CurrentThread.Name
        let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
        label1.Text <- label1.Text + sprintf "Here [%s] [%d]" threadName threadNumber

        // Switch back to the thread pool. 
        do! Async.SwitchToThreadPool()
        // Do something. 
        do! Async.Sleep(10000)
        let threadName = System.Threading.Thread.CurrentThread.Name
        let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
        label1.Text <- label1.Text +
                       sprintf "Switched to thread pool [%s] [%d]" threadName threadNumber
    }

let buttonClick(sender:obj, args) =
    let button = sender :?> Button
    Async.Start(async1(syncContext, button.Parent :?> Form))
    let threadName = System.Threading.Thread.CurrentThread.Name
    let threadNumber = System.Threading.Thread.CurrentThread.ManagedThreadId
    button.Parent.Text <- sprintf "Started asynchronous workflow [%s] [%d]" threadName threadNumber
    ()

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

Platformy

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Informacje o wersji

Wersje podstawowe biblioteki języka F#

Obsługiwane przez: 2.0, 4.0, przenośne

Zobacz też

Informacje

Control.Async — Klasa (F#)

Microsoft.FSharp.Control — Przestrzeń nazw (F#)