3 ms·
In a multi-threaded context yes, reduced memory usage is the main benefit. But async/await can also be used for other things! In a C# Windows GUI application,
by ynik 6y ago
In a multi-threaded context yes, reduced memory usage is the main benefit.
But async/await can also be used for other things! In a C# Windows GUI application, it's normal to use async/await on the UI thread. Your UI can await multiple tasks at the same time, yet you don't need any locks when accessing the UI state; because all your code runs on the UI thread.
This is a really useful programming model made possible by cooperative task-switching via `await` on a single thread.
Here the "await" being explicit is a crucial feature, it allows the programmer to reason about when the shared state might be mutated by other tasks (or maybe by the user clicking cancel while the current task is waiting).
Any pre-emptive task switching adds a lot of additional complexity and isn't really suitable for UI code.
- spaetzleesser 6y ago“Your UI can await multiple tasks at the same time, yet you don't need any locks when accessing the UI state; because all your code runs on the UI thread.” Is that true? I thought the code after each await runs on a different thread from the code before the await. At least that’s what I have observed when debugging things.