Tutorial by Examples

Three things are needed to use async-await: The Task object: This object is returned by a method which is executed asynchronously. It allows you to control the execution of the method. The await keyword: "Awaits" a Task. Put this keyword before the Task to asynchronously wait for it to...
If you want to execute synchronous code asynchronous (for example CPU extensive calculations), you can use Task.Run(() => {}). public async Task DoStuffAsync() { await DoCpuBoundWorkAsync(); } private async Task DoCpuBoundWorkAsync() { await Task.Run(() => { fo...
The Task object is an object like any other if you take away the async-await keywords. Consider this example: public async Task DoStuffAsync() { await WaitAsync(); await WaitDirectlyAsync(); } private async Task WaitAsync() { await Task.Delay(1000); } private Task WaitDire...
You can use void (instead of Task) as a return type of an asynchronous method. This will result in a "fire-and-forget" action: public void DoStuff() { FireAndForgetAsync(); } private async void FireAndForgetAsync() { await Task.Delay(1000); throw new Exception(); ...

Page 1 of 1