简体   繁体   中英

Is Async Await in Console apps as useful as GUI and ASP.NET apps?

I am currently learning C# Async/Await feature and can see its usefulness in GUI and web apps but I am still trying to figure out its real usefulness in Console apps. Can you give an example that drives home the point?

Async allows running more code until tasks are awaited, so if there is more code that can be run simultaniously (meaning - it is not dependent of the other task), it can start right away.

for example:

public async Task<string> GetUserFullNameAsync(string firstName)
{
   return await GetUserFullNameAsyncInner(firstName); // gets user name from db in an async fashion - takes 4 seconds
}

public async Task<DateTime> GetFlightTimeAsync(string filghtName)
{
   return await GetFlightTimeAsyncInner(filghtName); // gets filget time from db in as async fashion - takes 4 seconds
}

public async Task<UserDetails> GetUserDetailsAsync(string userFullName)
{
   return await GetUserDetailsAsyncInner(name); // gets user details by its full name from db in an async fashion - takes 4 seconds
}

lets look at this function:

public async <UserDetails> GetUserDetails(string firstName)
{
   var userFullName = await GetUserDetailsAsync(firstName);
   return await GetUserDetailsAsync(userFullName);
}

notice how GetUserDetailsAsync is dependent of getting the full name first, by using GetUserDetailsAsync . So if you need to get the UserDetails object, you are dependent of waitig for the GetUserDetailsAsync to finish. that may take some time - especailly for heavier actions like video processing and such. In this example - 4 seconds for the first function + 4 seconds for the seconds = 8 seconds.

now lets look at this second function:

public async <FlightDetails> GetUserFlightDetails(string firstName, string flightName)
{
   var userFullNameTask = GetUserDetailsAsync(firstName);
   var flightTimeTask = GetFlightTimeAsync(flightName);

   await Task.WhenAll(userFullNameTask, flightTimeTask);
   return new FlightDetails(await userFullNameTask, await flightTimeTask);
}

Notice that GetFlightTimeAsync is not dependent on any other function, so if you need say that user full name and flight time, you can do it in a parallel way, so both actions are processed in the same time - hence the total time to wait is faster than getting the full name and then getting the flight time. 4 seconds for the first function + 4 seconds for the second - in a parallel way < 8 seconds.

Let's look at a different angle on the asynchronous programming than just a way of doing things in parallel. Yes, you can run tasks in parallel but you can find so much code that is using await/async but it is waiting on every asynchronous execution.

What is the point of doing so? There is no parallel execution there...

It is everything about making better use of available system resources, especially threads.

Once the execution reaches an asynchronous code the thread can be released and the threads are limited system resources. By releasing the thread when it's idling for an IO-bound work to complete, it can be used to serve another request. It also protects against usage bursts since the scheduler doesn't suddenly find itself starved of threads to serve new requests. Choosing an async operation instead of a synchronous one doesn't speed up the operation. It will take the same amount of time (or even more). It just enables that thread to continue executing some other CPU bound work instead of wasting resources.

If you have any I/O-bound needs (such as requesting data from a.network, accessing a database, or reading and writing to a file system), you'll want to utilize asynchronous programming. No matter if the application is a console one or not.

Bonus : If you are wondering: "Ok, my application released the thread but there must be some other thread that is really doing the wait!" have a look at this article from Stephen Cleary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM