
We learned the Async Await pattern. The await keyword provides a non-blocking way to start a task, then continue execution when that task completes. whenever we want to use the await– we must add async modifier to the method signature. the async await pattern should be used when we are running operations such as: file access (read/ write), web operation, etc. the advantage of the async await is that we do not consume a thread to wait for the operation to complete. also need to take into consideration that the operations after the await are running in a background thread (so it is not guarantee to be completed). the next topic was UI thread in WinForm application. since blocking the UI thread causes the entire window to “freeze” we should always avoid making long operations on the UI thread. one solution could be to use the async await pattern. another solution could be to run long operation on a separate thread (Task) and call the UI thread using the BeginInvoke method on the specific control we want to redraw. why should we use the BeginInvoke? answer: since redrawing a component not from the UI thread causes the following exception: “…accessed from a thread other than the thread it was created on”
More topics covered:
- ILSpy extraction tool
- Async builds a state machine around the function
- Async key word does not break interface
- Async in signature return Task – automatically
- BeginInvoke on the main form
- Changing control color does not require the UI thread
- After the await the same thread may continue running the operations if it is available. if not, a different thread will continue
Links: