C# Xamarin and async

Lloyd Sheen 1,476 Reputation points
2022-02-13T22:41:06.93+00:00

Ok I have now spent the last few hours trying to get this to work. what I want to do is have two methods where are executed in parallel. Success is not a word I would use.

So I have these two metods:

public async static Task<List<Configurations>> GetConfigurationsAsync()
and
public async static Task<List<Expands>> GetExpandsAsync()

Now if I execute them like:

        var configs = await APICalls.GetConfigurationsAsync();
        var expands = await APICalls.GetExpandsAsync();

Now in the methods I create a string that has what happened and with the above code it returns (I have a Sleep of 10 seconds so I can see the UI and how it responds):

Start of GetConfigurations
End of GetConfigurations
Start of GetExpands
Start of GetExpands

So there is no async going on that I can see.

I then change the execute the methods code to:

        var configs = APICalls.GetConfigurationsAsync();
        var expands = APICalls.GetExpandsAsync();

        await configs;

This does nothing different so again no parallel tasks.

Also in both cases you cannot drag the window so it is locked.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,334 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,818 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,656 Reputation points
    2022-02-13T23:20:23.91+00:00

    Whenever you await a Task you're effectively creating a callback out of everything that occurs after that await statement and attaching it to the Task as a continuation.

    You don't necessarily need a continuation, although if you want to confirm from your calling code that the asynchronous execution completed successfully/with an error, or you want to use the result of the asynchronous action, you need to either await the response or attach a continuation (.ContinueWith().)

    In your first example this line:

    var expands = await APICalls.GetExpandsAsync();
    

    Is inside the continuation callback of the first, meaning it won't be dispatched until the first asynchronous call returns. The second snippet dispatches two asynchronous actions, but only awaits the first, so by the point that the continuation occurs the second task might not have completed.

    To await both both you need to use Task.WhenAll:

    var configs = APICalls.GetConfigurationsAsync();
    var expands = APICalls.GetExpandsAsync();
    await Task.WhenAll(configs, expands);
    
    // Access the values through "configs.Result" and "expands.Result"
    

    As for the second issue the reason the UI thread freezes is because the asynchronous action is being awaited inside a method that's called on the UI thread. The way around that is to await your async calls inside a Task.Run call which will cause the execution to occur on a new thread. Once your async action is done if you want to interact with UI elements you just need to make sure this is done on the UI thread, so you'll need to wrap any UI logic in a Device.BeginInvokeOnMainThread call. A better example is here:

    https://stackoverflow.com/questions/62959990/xamarin-forms-await-async-task-on-main-thread#answer-62987810

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.