Tutorial by Examples

public async Task<JobResult> GetDataFromWebAsync() { var nextJob = await _database.GetNextJobAsync(); var response = await _httpClient.GetAsync(nextJob.Uri); var pageContents = await response.Content.ReadAsStringAsync(); return await _database.SaveJobResultAsync(pageContents); } ...
6.0 As of C# 6.0, the await keyword can now be used within a catch and finally block. try { var client = new AsyncClient(); await client.DoSomething(); } catch (MyException ex) { await client.LogExceptionAsync(); throw; } finally { await client.CloseAsync(); } 5.06.0 P...
The web.config system.web.httpRuntime must target 4.5 to ensure the thread will renter the request context before resuming your async method. <httpRuntime targetFramework="4.5" /> Async and await have undefined behavior on ASP.NET prior to 4.5. Async / await will resume on an arb...
It is possible to await multiple calls concurrently by first invoking the awaitable tasks and then awaiting them. public async Task RunConcurrentTasks() { var firstTask = DoSomethingAsync(); var secondTask = DoSomethingElseAsync(); await firstTask; await secondTask; } Alt...
await operator and async keyword come together: The asynchronous method in which await is used must be modified by the async keyword. The opposite is not always true: you can mark a method as async without using await in its body. What await actually does is to suspend execution of the code ...
Methods that perform asynchronous operations don't need to use await if: There is only one asynchronous call inside the method The asynchronous call is at the end of the method Catching/handling exception that may happen within the Task is not necessary Consider this method that returns a Ta...
It is a bad practice to block on async calls as it can cause deadlocks in environments that have a synchronization context. The best practice is to use async/await "all the way down." For example, the following Windows Forms code causes a deadlock: private async Task<bool> TryThis()...
Consider the following code: public async Task MethodA() { await MethodB(); // Do other work } public async Task MethodB() { await MethodC(); // Do other work } public async Task MethodC() { // Or await some other async work await Task.Delay(100); } ...

Page 1 of 1