9

I'm getting my feet wet with .NET 4.5's async/await construct. I'm working on a RESTful Web API solution. I'm trying to figure out what to do with CPU-bound operation - 1) call it synchronously from the current thread, or 2) use Task.Run()?

Let's use the example from this page:

async Task<int> AccessTheWebAsync()
{ 
    // You need to add a reference to System.Net.Http to declare client.
    HttpClient client = new HttpClient();

    // GetStringAsync returns a Task<string>. That means that when you await the 
    // task you'll get a string (urlContents).
    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

    // You can do work here that doesn't rely on the string from GetStringAsync.
    DoCPUBoundWork();

    // The await operator suspends AccessTheWebAsync. 
    //  - AccessTheWebAsync can't continue until getStringTask is complete. 
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
    //  - Control resumes here when getStringTask is complete.  
    //  - The await operator then retrieves the string result from getStringTask. 
    string urlContents = await getStringTask;

    // The return statement specifies an integer result. 
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
    return urlContents.Length;
}

Here, let's suppose DoCPUBoundWork() is strictly CPU-bound and involves no IO of any kind.

Is it the best practice to call it from the current thread as shown?

Or is it better to have the following?

await Task.Run(() => DoCPUBoundWork()).ConfigureAwait(false);

I've read up on some of Mr. Cleary's posts, and am getting some mixed suggestions. In this post, he suggests that it's better to call the CPU-bound stuff synchronously to avoid the unnecessary overhead of async/await/Task.Run(). However, in this post, he suggests to use Task.Run() for CPU-bound operations without mentioning any exception cases. I'm sure I'm missing something obvious. Hoping to get some clarification on this.

2
  • How long your DoIndependentWork runs for typically? Commented May 22, 2015 at 18:51
  • Let's suppose it takes long enough to cause problems under high load...
    – Zoomzoom
    Commented May 22, 2015 at 18:55

1 Answer 1

7

Is it the best practice to call it from the current thread as shown?

If your current thread is free while the async operating is on going, why use a different thread? What would make that thread better than the one you're already using?

Question comes to mind concerning what DoIndependentWork is actually doing. If it's vital that it completes before the HTTP request finishes, I would invoke it synchronously. If the work isn't vital to complete before the HTTP request completes, than I would look for an entirely different solution. Using Task.Run is dangerous in ASP.NET.

Remember that continuations in ASP.NET run on arbitrary thread pool threads.

7
  • So... when does it make sense to offload it to another thread using Task.Run()? (I'm confused why Stephen Cleary suggests to use Task.Run() for CPU-bound stuff in his blog)
    – Zoomzoom
    Commented May 22, 2015 at 18:58
  • what about ui-bound threads in wpf? firing of an extra task to avoid blocking of the gui would be feasible...
    – user57508
    Commented May 22, 2015 at 18:58
  • 2
    @Andreas You're right, in case this was WPF I would advise differently. But this is ASP.NET, which is different. Commented May 22, 2015 at 19:00
  • 2
    @Zoomzoom In ASP.NET, you shouldn't even use Task.Run. It doesn't register work, and IIS may recycle your app while it's executing. Using Task.Run in UI based applications makes more sense and i'm assuming that's what Stephan was referring to. Commented May 22, 2015 at 19:04
  • 1
    I should have read the comments under his blog post. Someone had already asked this same question and Stephen did say something in agreement with you.
    – Zoomzoom
    Commented May 22, 2015 at 19:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.