Why should I use asynchronous operation for synchronous operation?

I have always considered this.

Say we have a simple asynchronous web request using the HttpWebRequest class

class webtest1 { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("www.google.com"); public webtest1() { this.StartWebRequest(); } void StartWebRequest() { webRequest.BeginGetResponse(new AsyncCallback(FinishWebRequest), null); } void FinishWebRequest(IAsyncResult result) { webRequest.EndGetResponse(result); } } 

The same can be achieved with synchronous operation:

 class webtest1 { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("www.google.com"); public webtest1() { webRequest.GetResponse(); } } 

So why would I want to use a more confusing asynchronous operation when a simplified synchronization operation would be sufficient? To save system resources?

+4
source share
6 answers

A synchronous operation will not allow you to do anything else, waiting for the request or timeout to complete. Using an asynchronous operation will allow you to revive something so that the user can show that the program is busy or even allows them to work with other areas of functionality.

+6
source

If you run the request asynchronously , you can do other things while you wait for an answer to your request. If you run the request synchronously , you will have to wait until you get a response until you can do something else.

For simple programs and scripts, this may not matter much, because in many of these situations the easiest way to code and understand the synchronous method will be the best design choice.

However, for non-trivial programs, such as a desktop application, a synchronous request that blocks the entire application until the request completes causes an unacceptable expansion of the user .

+9
source

The synchronous version is easier to code, but it masks a very serious problem. Network communication or indeed an I / O operation can be blocked for a long time. For example, many network connections have a timeout of 2 minutes.

Performing a network operation synchronously means that your application and user interface will be blocked for the entire duration of this operation. Unusual network hiccups can block your application for several minutes without the possibility of cancellation. This leads to very unhappy customers.

+6
source

Asynchronous becomes especially useful when you have more things than you have kernels - for example, you can have several active web requests, several file access operations and several database calls, as well as some other network operations (Maybe WCF or redis). If all of them are synchronous, you create many threads, many stacks and suffer from many context switches. If you can use the asynchronous API, you can usually use pool threads for short moments when each operation does something. This is great for high-end server environments. Having multiple cores is excellent, but more efficient.

In C # 5, this becomes more work through await than your second example.

+4
source

I read this the other day, and a similar question was considered earlier:

Performance Difference Between a Synchronous HTTP Handler and an Asynchronous HTTP Handler

+2
source

1) You are attached to a single-threaded environment , for example, silverlight. Here you have no choice but to use asynchronous calls, or the entire user stream will be blocked.

2) You have many challenges that take a long time. Why block the entire thread when it can continue to work and do other things, waiting for a return? For example, if I have five function calls, each of which takes 5 seconds, I would like to immediately start all of them and return them if necessary.

3) Too much data to process the output synchronously. If I have a program that writes 10 gigabytes of data to the console, and I want to read the result, I have an asynchronous asynchronous process that will execute line by line. If I do this synchronously, then I will run out of free space and close the program.

+1
source

Source: https://habr.com/ru/post/1386202/


All Articles