问题
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?
回答1:
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
.
回答2:
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.
来源:https://stackoverflow.com/questions/37785693/when-to-use-task-run-when-to-use-async-await-and-when-to-use-them-in-combinati