What is “Asynchronous” technique ?

Aykut Aktaş
Innovile
Published in
3 min readMay 2, 2022

--

You need to know about the general topics. When you want to look at the whole topic with an overview, please review the detailed information on these topics.

Asynchronous programming in C# — Understand async and await
Asynchronous Programming in C# — Return Type of Asynchronous Method
Asynchronous Programming in C# — Understand Task in Asynchronous programming.
Asynchronous Programming in C# — Exception Handling in Asynchronous Programming
Asynchronous Programming in C# — Access Data in Asynchronous Functions

If you are a senior/expert web developer (at least 5+ years in this field) then you have a lot of experience with the bad response time of web applications.

Yes, there was no way to change a small part of the content without reloading the page. And this situation will be more difficult with a slow internet connection.

But, this situation changing with async programming. We can do this without reloading an entire page or without touching another element. User’s experience and performance have increased.

So, how is it done? You are thinking, with AJAX, right? Yes with AJAX, simply one asynchronous technique is needed to exchange data between the server and the client.

The ultimate goal of AJAX is to call a server method and exchange data from the server without hampering the client. The clients no need wait for the server’s response.

So, asynchronous programming is also all about improvement of performance. Basically, we can implement Ajax in two ways (in ASP.NET).

The first option is by updating the panel and Ajax toolkit.
The second option is by the jQuery Ajax method. (Let’s ignore the various third-party JavaScript libraries).

In C# 5.0 Microsoft has given us the ability to write our own asynchronous code with C#.

“Async”
This keyword is used to qualify a function as an asynchronous function.
In other words, if we specify the async keyword in front of a function then we can call this function asynchronously.

“Await”
Very similar to wait, right? Yes, this keyword is used when we want to call any function asynchronously.

If you’re relatively new to the concept of asynchronous programming then this dry definition is not enough to understand those concepts.
You need write to one small example and you can try to understand those concepts.

The return type of an asynchronous function is Task. In other words, when it finishes its execution it will complete a Task. Each and every asynchronous method can return three types of values.

Void: Means nothing to return

Task: It will perform one operation, a little similar to void but the difference is there.

Task<T>: Will return a task with a T type parameter. (I hope you are familiar with the concept of T)

Let’s clarify a few more concepts here:
- The Main method cannot be defined as asynchronous.
- It (the Main method) cannot be invoked by the await keyword.
- If any asynchronous method is not invoked by the await keyword then by nature it will behave like the synchronous method.
- Function properties should not be defined as asynchronous.
- The await keyword may not be inside a “lock” section.
- A try/catch block should not call any asynchronous methods from the catch or finally block.
- A class constructor or destructor should not define an asynchronous method nor should it be called asynchronously.

Return void from asynchronous method
Though it’s not recommended to return void from an asynchronous function, we can return void theoretically. Now, the question is, why is it not recommended to return void? The answer is if we return void then the caller function will not be informed of the completion of the asynchronous function. OK, then in which scenario can we return void? When we want to call an asynchronous function from an event (like a button click) then we can specify void in the event.

Return Task from asynchronous method
Basically the returning task is nothing but sending one signal to the caller function that the task has finished. When a method returns a task, we can use the await keyword to call it.

Implement try-catch within the function
Implement a try-catch block within an asynchronous function. This is the solution to catch exceptions in asynchronous methods.

Github Examples
Asynchronous Programming with async and await (C#)Describes how to write asynchronous solutions by using the async and await keywords in C#.

Link : https://github.com/dotnet/docs/blob/main/docs/csharp/programming-guide/concepts/async/index.md

--

--