async Task Main() + GUI

Hi,
I just noticed the very nice feature with async Task Main.
However, the following piece of code acts a little unexpected.
//[STAThread] async Task Main() { await Task.Delay(TimeSpan.FromSeconds(1));// .ConfigureAwait(true); new Button(){Content = "A"}.Dump(); }
I would expect it to return to the execution context of the ui thread.
In comparison, this works:
async Task Main() { var disp = Dispatcher.CurrentDispatcher; await Task.Delay(TimeSpan.FromSeconds(1));//.ConfigureAwait(true); disp.Invoke(() => { new Button() {Content = "A"}.Dump(); }); }
Also is there a way to get the ui dispatcher so that one could dispatch work to the ui thread of the query? I noticed it is possible to just get the current dispatcher at the top of the query but that feels a little bit off for larger queries.

Thanks in advance.

Comments

  • You need to ensure that a synchronization context is created before the first await.

    You can do this by dumping a UI control before the first await, or by putting the following code at the start of your query:

    Util.CreateSynchronizationContext();
  • Ah, I see.
    Thanks again.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!