Async/await

Asynchronous programming was first released for Microsoft Visual Studio 2012 as an add-on, and is now natively available in Microsoft Visual Studio 2013. Asynchronous programming is also available with a special pattern called async/await, which is greatly optimized for cross-thread operations.

This pattern helps to achieve asynchronous programming in a simplified way and adds a transparent (to programmers) asynchronous/synchronous jump, row-by-row, with the ability to execute code on the UI, creating threads without having to use any dispatcher or a delegate. The following is an example from the legacy Windows Forms as seen earlier:

public Form1()
{
    InitializeComponent();

    OnAsyncWay();
}

private async Task OnAsyncWay()
{
    //running in creating thread

    //async elaboration
    //this starts a new task that returns the required value
    var text = await Task.Factory.StartNew(() =>
    {
        //running on another thread
        Thread.Sleep(1000);
        return "Hi";
    });

    //running again on creating thread
    label1.Text = text;
}

As it is visible (although usually the StartNew syntax returns a task), this is executed by the await method that translates it in the asynchronous returned values as the Result property of the task itself.

Tip

Any async method must await something, else it is actually useless.

The async keyword specifies that the whole method contains asynchronous calls. The await keyword, instead, specifies that the next invocation will execute asynchronously, and when such a task ends, the code must continue again in a synchronous way on the caller thread. Unlike a dispatcher, that requests another thread that is doing something, with async/await methods we can write code that can work on multiple threads in a very simplified way.

We can also await multiple tasks with the Task.WhenAll method, as follows:

private async Task OnAsyncWay()
{
    var allValues = await Task.WhenAll(
        Task.Factory.StartNew<int>(TaskRunner1),
        Task.Factory.StartNew<int>(TaskRunner2),
        Task.Factory.StartNew<int>(TaskRunner3)
        );
}

private int TaskRunner3() { return 3; }
private int TaskRunner2() { return 2; }
private int TaskRunner1() { return 1; }

When dealing with async/await methods, if sleep-time is needed, instead of using the Thread.Sleep methods that occur on the main thread, use the Task.Delay method, which will wait for the same time without having to block the calling thread. The following is an example:

await Task.Delay(1000);

Keep in mind that async/await methods add the thread switching feature to the existing task-based asynchronous programming techniques already seen earlier. Everything is still available, from task continuation to factory configuration, task child synchronization, and so on. With such added features, it becomes very easy to deal with UI updates. Asynchronous programming is necessary to achieve a greater user experience and high throughput/scalability for any application.

For further reading, you can refer to the following link https://msdn.microsoft.com/en-us/library/hh191443.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.72.74