1

I know a little about async and await in c#. But I saw this method of returning the view as shown. So I would like to know how this method works?

I have done this,

public async Task<PartialViewResult> SamplePartialPage()
{
    ViewBag.countryList = await db.CountryList.ToListAsync();
    return PartialView();
}

but not this.

public async Task<PartialViewResult> SamplePartialPage()
{
    ViewBag.countryList = await db.CountryList.ToListAsync();
    return await Task.Run(() => PartialView());
}
Thameem
  • 700
  • 1
  • 13
  • 38
  • 4
    You should change the return type to `PartialViewResult` instead of `Task` – Sisir Jun 13 '20 at 06:13
  • sure. I too felt like it was not necessary. but i would like to know why do some people use the second behavior? – Thameem Jun 13 '20 at 06:33
  • 2
    @Thameem When they need to force the code to execute on another thread and/or when they don't quite have a clue about what they're doing. – GSerg Jun 13 '20 at 07:22
  • I've seen this in the past where the method at one point had no asynchronous calls (`await`) and the author *unfortunately* added the `await Task.Run` in order to silence the compiler warning rather than de-Taskifying the method (removal of `async` and modified return type). It *could* be the case here that the method was once fully sync and it was not fixed when async population of `countryList` was introduced. But it's hard to speculate about other people's intentions... – pinkfloydx33 Jun 13 '20 at 10:26
  • 2
    @Sisir If they did that then the method could not be `async` and they would not be able to `await` the `ToListAsync`. – juharr Jun 13 '20 at 11:33

1 Answers1

5

I know a little about async and await in c#.

I recommend reading my async intro and async best practices. There are also lots of other great resources. Since you're on ASP.NET, you should also read async on ASP.NET.

i would like to know why do some people use the second behavior?

On ASP.NET, there isn't a good reason to use await Task.Run. await Task.Run schedules work to a thread pool thread and frees up the current thread. But on ASP.NET, the code was already running on a thread pool thread, so await Task.Run just causes a thread switch and provides no benefits at all.

This is a valid pattern in other scenarios; specifically, if you're on a UI thread in a client-side GUI app, then using await Task.Run is a normal pattern to push work off the UI thread and keep it responsive.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Also the example looks like [I/O-bound work](https://learn.microsoft.com/en-us/dotnet/csharp/async#recognize-cpu-bound-and-io-bound-work). – aepot Jun 13 '20 at 12:41
  • await Task.Run just causes a thread switch and provides no benefits at all. && This is a valid pattern in other scenarios; specifically, if you're on a UI thread in a client-side GUI app, then using await Task.Run is a normal pattern to push work off the UI thread and keep it responsive. – Thameem Jun 13 '20 at 12:48
  • 1
    @aepot: Creating the view is CPU-bound, but it is so short that even if you *wanted* to free up the thread via `Task.Run`, the overhead may make the result slower anyway. – Stephen Cleary Jun 13 '20 at 16:21