• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

Async/Await Tutorial

Mitch528

Respected Member
In .NET Framework 4.5, the new keywords async/await were introduced. This is a big deal. These keywords drastically reduce the complexity of writing asynchronous code. It used to be (in .NET Framework 4.0) that you had to use either TPL (Task-based Asynchronous Pattern), EAP (Event based Asynchronous Pattern), or APM (Asynchronous Programming Model). I won't go into detail about those methods, but let's just say that they were more complicated and takes up a lot more lines of code than when using the new async/await.

Before I get into how it works, you may be wondering what this whole asynchronous thing is all about. Let's say, for example, that I'm writing a windows application and I want to perform a long running operation, such as downloading a file. When downloading the file synchronously (not using asynchronous), you may notice that your application will freeze until the file download has been completed. This is normal. Since the operation is being run on the UI thread, it has to wait until the download has been completed until it can resume normal functionality. This is where asynchronous comes in. Basically, using an asynchronous method will not block the UI from running normally while downloading the file.


Async/await is amazingly simple to use. Here's an example:


Code:
public async void button_click(object sender, EventArgs e) {

    await RunLongOperation();

    myTextBox.Text = "Operation completed!";

}

public async Task RunLongOperation() {

    return Task.Run(() => {

        for (int i = 0; i < 10000; i++) {
             //do something here
        }

    });

}


Okay, so, this may be a bit confusing at first, so let's break it down.

Code:
public async void button_click(object sender, EventArgs e) {

Notice the async keyword on the method header. This just tells the compiler that there will be an await keyword in the method body.


Code:
await RunLongOperation();

Here we use the await keyword before the method call. This means that the method will wait until this operation has been completed before moving on. Even though it "waits", it will NOT block the current thread, which in this case is the UI thread.


Code:
public async Task RunLongOperation() {

Notice that we again use the async keyword in the method header.

However, in this case, we also return a Task object. This enables us to use the await keyword when calling the method.


Code:
return Task.Run(() => {

    for (int i = 0; i < 10000; i++) {
         //do something here
    }

});


In this part, we run an asynchronous Task in which we do our long-running operation and immediately return the Task object.



However, what if you want to return a value after the long-running operation completes? Well, it's easy! All you have to do is:

Code:
public async Task<int> RunLongOperation() { //int as an example

    return Task.Run(() => {

        int counter = 0;

        for (int i = 0; i < 10000; i++) {
             counter++;
        }

        return counter;

    });

}

and

Code:
public async void button_click(object sender, EventArgs e) {

    int counter = await RunLongOperation();

    myTextBox.Text = "Operation completed: " + counter;

}


And that's basically it.
 

Tommy

Founder
Yeah, the new keywords are very helpful because I ALWAYS had to write async custom code, especially when using async sockets and it was horrifying. Glad 4.5 introduced it because it made my world upright. :D

Thanks for the small guide.
 
Top