Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I did this for our <a href="http://www.ffconsultancy.com/products/fsharp_for_visualization/" rel="noreferrer">F# for Visualization</a> library and then described the technique I used in my book <a href="http://ffconsultancy.com/products/fsharp_for_technical_computing/" rel="noreferrer">Visual F# 2010 for Technical Computing</a>.</p> <p>Firstly, I wrote a lazy thunk that initializes WPF (including an STA UI thread and <code>Application</code>) when its evaluation is forced:</p> <pre><code>&gt; let ui = let mk() = let wh = new ManualResetEvent(false) let application = ref null let start() = let app = Application() application := app ignore(wh.Set()) app.Run() |&gt; ignore let thread = Thread start thread.IsBackground &lt;- true thread.SetApartmentState ApartmentState.STA thread.Start() ignore(wh.WaitOne()) !application, thread lazy(mk());; val ui : Lazy&lt;Application * Thread&gt; = &lt;unevaluated&gt; </code></pre> <p>Then I wrote a <code>spawn</code> function that dispatches the application of a function <code>f</code> to an argument <code>x</code> such that it is run on the UI thread:</p> <pre><code>&gt; let spawn : ('a -&gt; 'b) -&gt; 'a -&gt; 'b = fun f x -&gt; let app, thread = ui.Force() let f _ = try let f_x = f x fun () -&gt; f_x with e -&gt; fun () -&gt; raise e let t = app.Dispatcher.Invoke(DispatcherPriority.Send, System.Func&lt;_, _&gt;(f), null) (t :?&gt; unit -&gt; 'b)();; val spawn : ('a -&gt; 'b) -&gt; 'a -&gt; 'b </code></pre> <p>Now it is just a case of invoking your <code>openAWindow</code> function on the UI thread with:</p> <pre><code>let openAWindow text = DisplayWindow().SetMessage text spawn openAWindow text </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload