Guest User

Untitled

a guest
May 7th, 2012
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment