Putting the fun in functional programming since 2005!
Sunday, 6 May 2012
How do I spawn a WPF UI from F# interactive asynchronously?
F# interactive is a fantastic tool for interactive technical computing and one of the first things many users want is the ability to fire up WPF, create new windows and control the content in them from the F# interactive session. This blog post describes one solution.
Firstly, write a lazy thunk that initializes WPF (including an STA UI thread and Application) when its evaluation is forced:
Then write a spawn function that dispatches the application of a function f to an argument x such that it is run on the UI thread:
>let spawn :('a ->'b)->'a ->'b =fun f x ->let app, thread = ui.Force()let f _ =trylet f_x = f x
with e ->fun()-> raise e
let t = app.Dispatcher.Invoke(DispatcherPriority.Send, System.Func<_, _>(f),null)(t :?> unit ->'b)();;val spawn :('a ->'b)->'a ->'b