T
T
tnnmi2016-09-01 21:40:02
C++ / C#
tnnmi, 2016-09-01 21:40:02

Async and Await C# on a Thread (Thread) without events. If necessary?

I'm trying to figure out where you can use async and await, and where it's better to do without it.
Do I understand correctly that in a "simple" thread that does not use the event model, it makes no sense to use async and await. For example, reading a file from disk into a MemoryStream. Let's say nothing happens in the stream except reading the file - then it's better to read it synchronously?
In fact, as Haze Max wrote, Async and Await are best applied on the window thread.
There is a question: and for flows which use events (for example on the timer)?
I just want to be 100% sure

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Larry Underwood, 2016-09-02
@tnnmi

Async / Await is sugar for optimal use of the thread pool, you can do without it, and if you need to use all the threads to their fullest, then you should use Async / Await.
Offhand, I will only say such situations where it is necessary:
​​1) To get rid of synchronous calls of long calculations in threads that are critical to execution speed. Just a special case - an example that Haze Max described . But we must not forget that in WPF and WinForms, in order to do something with the window after these calculations, you need to marshal back to the UI thread.
2) Intensive IO - in order not to stop a thread that is waiting for data (or writing data) from an external resource (hdd, network card for example), code execution should be switched to asynchronous mode in order to get rid of the thread freezing for the waiting time (while waiting for a response , the thread may execute some other code).

H
Haze Max, 2016-09-01
@hazemax

Async and Await are just better to use on the window thread so that the window does not "hang". But if there is already a separate thread for reading from the file, then there is no point in Async and Await, it is better to read it synchronously.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question