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.