C# language: Asynchronous

This lesson will require knowledge from the previous one, so if you do not know what concurrency is, go to the concurrency lesson and then come back to this one. Today about asynchronous.

 

Synchronous vs. asynchronous operations

A synchronous operation does its job before passing the control back to the object or the method that called it.

The asynchronous operation, on the other hand, performs (most) work after transferring control back to the component that called it.

The large majority of the methods you will work with will be synchronous methods, for example, List.Add(), Console.WriteLine(), etc.

Asynchronous methods are less common and initiate concurrency because they work in parallel with the component invoking them.

Asynchronous methods that we have used so far are, for example:

1. Thread.Start().
2. Task.Run().
3. Methods joining the continuation tasks.

 

What is asynchronous programming?

The principle of asynchrony is that long-running functions are created in an asynchronous manner, ie they are created in a new thread or task in order to apply concurrency.

In an asynchronous approach, there are two benefits:

1. Conversion can be implemented without threads and as I said in the previous one, which means improved scalability and performance

2. Large applications contain a smaller amount of code, which increases readability and security.

 

Asynchronous functions in C #

Asynchronous programming introduces two key words async and await keyword, they allow to create asynchronous code, which will have the same structure and simplicity as the synchronous code, and eliminate complexity in the asynchronous code.

For example, the code in the form below:

It is extended by the compiler to the form of functionality similar to the following:

So, as you can see, it’s less complex.

We will return to this example from the previous lesson:

With the use of the async and await keywords, the above example will look like this:

So as you can see the await keyword simplify very much getting the continuation and legibility of the code, you must always remember about the async keyword in the name of the method, so that it can compile.

We will now see the use of asynchrony in practical solutions, create Windows Forms applications and make a synchronous application in the main file, paste this code:

This is an example related to calculations, after clicking the “Calculate prime numbers” button, the application hangs, you can not do anything until the application has finished calculating.

So how do you make the application asynchronous so that it does not hang on the calculation?

You have to convert it in this way:

We have changed the GetPrimesCount() method on asynchronous and we redesigned the function of clicking the button to call the GetPrimesCount() method.

This content also you can find on my steemit blog: https://steemit.com/programming/@slawas/c-language-asynchronous

And on medium: https://medium.com/@sawomirkowalski/c-language-asynchronous-dbf572e61805

 
If that post was useful for you share it with your friends :)

Post a comment

Be first!

avatar
  Subscribe  
Notify about