0
var categories1 = _context.Categories.ToList();
var categories2 = _context.Categories.ToListAsync().Result;
var categories3 = await _context.Categories.ToListAsync();
var categories4Task = _context.Categories.ToListAsync();

// do some other work
var categories4 = await categories4Task;

In above fours statements, first statement is normal and in other three statements, asynchronous programing is used.

As per my knowledge, if we use async task and then to do some other work after the async task as done in categories4Task variable, it is useful as it will work in parallel and total execution time will be saved.

But in case categories2 we are taking result in same line by using Result property and also in categories3 we are using async and await in same line. How is this beneficial for us to use async and await in same line? Is this equal to categories1 query or not?

Can anyone explain please?

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 3
    `How is this beneficial for us to use async and await in same line?` Tell us how much you understand about async and await so we can pitch the answer at a level you can understand. – mjwills Jul 18 '21 at 06:52
  • 1
    *In above fours statements, first statement is normal and in other three statements, async programing is used.*: How is the second statement asynchronous? Do you understand the implications of using `.Result`? – Peter Bons Jul 18 '21 at 07:09
  • *"in categories3 we are using async and await in same line."* I am seeing the [`await`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/await), but I am not seeing the [`async`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/async). – Theodor Zoulias Jul 18 '21 at 08:24
  • @TheodorZoulias They are referring to the `Async` in the name of the method. – mjwills Jul 18 '21 at 08:48
  • The main difference between categories1, which is obtained using a synchronous call, and categories3, is that categories3 is obtained using an asynchronous call, and since you're using `await`, the method containing this statement will also be an `async` method. The main thing you need to read more about is what `async/await` **does** to a method, as on the surface these two statements both look rather similar but they have drastically different effects on how the method *containing* these statements is compiled. – Lasse V. Karlsen Jul 18 '21 at 12:24
  • Also, never use `.Result` unless you're in a piece of code executing **only** when the task you use it on is **guaranteed** to have completed. For instance, after that last `await` statement in your question, it's safe to read `.Result` on `categories4Task`, because you know the code got here only *after* the task completed. – Lasse V. Karlsen Jul 18 '21 at 12:24

2 Answers2

2

.Result (and .Wait()) should be avoided except for some very very niche and nuanced scenarios. Basically: don't do that. It is actively harmful, and even when it doesn't bite you: it throws away all the benefits of async, but keeps the costs.

Using await immediately on a possibly-incomplete operation is normal and expected. Do that!

The last scenario, where you start an operation then do other things before awaiting it is complex. In pre-async code, that's the equivalent of starting a second concurrent thread, as you now have 2 execution flows running side-by-side. This can be OK, but you need to think very seriously about the concurrency implications. This approach should not be used casually.

So:

  1. Fine, normal sync code
  2. Do not use
  3. Fine, normal async code
  4. Possibly concurrent async code; use with caution
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
1

Asynchronous operations such as ToListAsync, SingleAsync, FirstOrDefaultAsync avoid blocking a thread while the query is being executed in the DB or any other I/O operation. such operations are important because they allow to free up the thread so it can service other incoming requests. they also keep a responsive UI in client applications.

EF Core provides asynchronous alternatives to the synchronous methods which perform I/O operations. they have the same effects as the synchronous methods, and can be used with the await keyword (the method must be marked with the async keyword)

Ran Turner
  • 14,906
  • 5
  • 47
  • 53