How to create a STA thread for F# Asynchronous workflow

85 Views Asked by At

I am attempting to write a printed form in F# using WPF controls—-and do so in an asynchronous workflow. Being new to this, I can’t figure out how to place the wpf controls, consumed by the F# asynchronous block, on a STA thread, without blocking the main thread.

I have a lot of printed forms and do not want to block the main thread.

How is a STA thread created for the F# asynchronous workflow?

Thanks in advance for any help.

1

There are 1 best solutions below

4
tranquillity On

In pure WPF I don't think you have STA - that concept comes from the COM model. What you do have is a specific UI thread that must be used for communicating with the UI. To get that thread you can use SynchronizationContext.Current property when you are certain you are on the UI thread (e.g. in an event handler). Note: If SynchronizationContext.Current is null then you will be running in the thread pool rather than on the UI thread.

If you then pass that SynchronizationContext object into your async {} code, you should be able to rejoin the UI thread by calling Async.SwitchToContext

Related Questions in F#