Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.95 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. F# Display a WPF window asynchronously
  2. > let ui =
  3.     let mk() =
  4.       let wh = new ManualResetEvent(false)
  5.       let application = ref null
  6.       let start() =
  7.         let app = Application()
  8.         application := app
  9.         ignore(wh.Set())
  10.         app.Run() |> ignore
  11.     let thread = Thread start
  12.     thread.IsBackground <- true
  13.     thread.SetApartmentState ApartmentState.STA
  14.     thread.Start()
  15.     ignore(wh.WaitOne())
  16.     !application, thread
  17.   lazy(mk());;
  18. val ui : Lazy<Application * Thread> = <unevaluated>
  19.        
  20. > let spawn : ('a -> 'b) -> 'a -> 'b =
  21.     fun f x ->
  22.       let app, thread = ui.Force()
  23.       let f _ =
  24.         try
  25.           let f_x = f x
  26.           fun () -> f_x
  27.         with e ->
  28.           fun () -> raise e
  29.       let t = app.Dispatcher.Invoke(DispatcherPriority.Send, System.Func<_, _>(f), null)
  30.       (t :?> unit -> 'b)();;
  31. val spawn : ('a -> 'b) -> 'a -> 'b
  32.        
  33. let openAWindow text =
  34.   DisplayWindow().SetMessage text
  35.  
  36. spawn openAWindow text