2009-07-12 5 views
2

Considérons le fragment de code suivant:STAThread comme flux de Async F #

 
let t1 = async { return process1() } 
let t2 = async { return process2() } 
let t3 = async { return windowsProcess() } 

let execute = 
    [ t1; t2; t3 ] 
    |> Async.Parallel 
    |> Async.RunSynchronously 
    |> ignore 

Que faire dans le cas où le windowsProcess a besoin d'un STAThread? Est-ce possible avec cette construction?

Répondre

5

S'il existe un thread particulier avec un SyncrhonizationContext, par ex. le fil de l'interface utilisateur, alors vous pourriez faire, par exemple.

// earlier on the UI thread... 
let syncCtxt = System.Threading.SynchronizationContext.Current 

// then define t3 as 
let t3 = async { 
    do! Async.SwitchToGuiThread(syncCtxt) 
    // now we're running on the UI thread until the next async 'bind' point 
    return windowsProcess() 
    } 

mais en général les tâches parallèles démarreront dans le pool de threads.