1

I have read many posts but still unable to differentiate between all these. All i could understand was Task.Run will call the background thread. async- await is asynchronous programming.

Does Task.Run means background thread will behave as a blocking thread?

Trying to download multiple large images from internet. How should i use these keywords in combination and why?

knowdotnet
  • 839
  • 1
  • 15
  • 29
  • In web applications (ASP.NET), async/await is not much useful because each request gets its own thread and none of the threads are blocking as with desktop and mobile application. Can you please state with what technology you're building the application (UWP/WPF/ASP/WF...)? – Aleksandar Toplek Jun 13 '16 at 09:18
  • i am trying to create a desktop console application in .net 4.5 – knowdotnet Jun 13 '16 at 09:23
  • Use async/await for IO ops. Task.Run will starve the thread pool. – andrei.ciprian Jun 13 '16 at 09:38
  • 1
    @AleksandarToplek - async/await is very much useful in web applications. It will release threads to serve other requests while waiting on IO / DB etc. The number of threads available for the application pool is limited. – smoksnes Jun 13 '16 at 09:50
  • @smoksnes I can't edit my response, didn't mean that they are not useful, but that they are not blocking in same way as with UI applications. – Aleksandar Toplek Jun 13 '16 at 09:51

2 Answers2

3

When to use Task.Run, when to use async- await and when to use them in combination

I have an entire blog series on the subject, but in summary:

  • Use Task.Run to move CPU-bound (or blocking) work off of the UI thread for apps with a UI.
  • Use async/await for I/O-bound work.

There are some more rare cases where Task.Run or async can be useful, but the two points above provide guidance for most situations.

Does Task.Run means background thread will behave as a blocking thread?

Task.Run can actually run synchronously or asynchronously, but it does use thread pool threads.

i am trying to create a desktop console application in .net 4.5

Trying to download multiple large images from internet. How should i use these keywords in combination and why?

This is an I/O-bound operation in a non-UI app, so you should definitely prefer async/await.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
0

You can call the

await Task.Run(DownloadImageMethod);

This will not block your calling thread when images are downloading and will continue executing following code when downloading is finished.

Task.Run(DownloadImageMethod);

This will not block your calling thread when images are downloading but will continue executing following code right await. This will not wait for downloading to finish before executing next method/command.


Take a look at similar questions here on SO For example:

Aync/Await action within Task.Run()

TLDR;

Task.Run will create new thread and start it. This thread will run asynchronously.

If you want the thread to run synchronously, you cant just call Task.Wait() method on it.

If you want the thread to run asynchronously but pretend like it's synchronous, you can put await (await Task.Run(...)) before it. This will allow you to write code in sequential way and you don't have to deal with callbacks.

When you do start new thread with Task.Run that thread will free up your current thread and you can do other work in it. I don't see how this would help in your case with console application.

Good example is UI thread in desktop and mobile applications where rendering is done on UI thread and you wouldn't want to stop the UI thread from doing its thing while you're downloading image.

In that case, await Task.Run(...) would start another thread, continuing other work on UI thread and when task is done, return you to the UI thread where you can manipulate controls and update UI related properties.

-- EDIT

Use Task.Run only when necessary - only when method that you call within Task.Run would be running for more than 0.5s. This will prevent application to "freeze" when you do IO operations.

Community
  • 1
  • 1
Aleksandar Toplek
  • 2,792
  • 29
  • 44