Search code examples
c#.netwpfasync-awaitdispatcher

WPF async in Current Dispatcher


I'm updating my WPF app from .net 4.6 Framework to .net 6. For some reason I can't find the correct way to execute async code in the dispatcher.

private async Task HandleNotFromUI()
{
  await Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, SomeMethodAsync);
}
private async Task SomeMethodAsync()
{
   someCode();
   await someAsyncCode();
   someMoreCode();
}

And it "looses" the thread on await someAsyncCode and does not hit someMoreCode. Similar code was working fine before updating to .net 6. Is it something about .net 6 or WPF in .net 6 or I'm making some mistake?


Solution

  • Changed Dispatcher.CurrentDispatcher to App.Current.Dispatcher and it is working. I'm not sure why though.