When no result value is needed, an async Task method is better than an async void method because it allows the caller to also use await to wait for it, making ordering and exception handling easier. Let's start by updating this code so that the thread doesn't block while tasks are running.
That signals to the compiler that this method contains an await statement; it contains asynchronous operations.
The core actions are still clear from reading the code. You should use void when you know that no caller will ever need to know when the operation is finished or whether it succeeded. Before serving breakfast, you want to await the task that represents toasting the bread before adding butter and jam.
Once the asynchronous method completes the execution, the async methods updates the state to Task object marking it completed and the string value is returned to msg variable.
Consider applying the await operator to the result of the call.
It looks much more like the original synchronous version of the code. Sometimes the wrong Task can be waited on. All rights reserved. Some C syntax features can help here—we can use a local method declaration InnerMethod in the example.
Async in C 5. Your application shouldn't make a phone appear frozen while it's downloading data from the web. You compose tasks by separating the operations into a new method that returns a task.