Search code examples
async-awaituno-platform

Uno platform: invoke UI update from async thread


I have an async member which ultimately needs to invoke some UI updates, after getting some data from a server.

I think I need something like BeginInvokeOnMainThread, or Dispatcher.Invoke, but neither of these appear to be recognized in the Uno context.

Here's the essence of what I have:

public async Task LoadList()
{
  ...
  // get data
  Uri uri = new Uri("https://...");
  response = await httpClient.GetAsync(uri);

  // display
  BeginInvokeOnMainThread () =>
  {
    ... update the UI ...
  });
}

But I get the error CS0103 The name 'BeginInvokeOnMainThread' does not exist in the current context UnoTest.Droid, UnoTest.UWP, UnoTest.Wasm


Solution

  • BeginInvokeOnMainThread / Dispatcher.Invoke / Control.Invoke were never a good idea.

    Uno should have a SynchronizationContext that is automatically used by await, so manual thread marshaling should not be necessary:

    public async Task LoadList()
    {
      ...
      // get data
      Uri uri = new Uri("https://...");
      response = await httpClient.GetAsync(uri);
    
      // display
      ... update the UI ...
    }