Welcome to the FoxLearn
Today we will learn how to use async and await
First, You need to add a Button and TextBox to Form1
The TextBox displays some messages when you click start button
You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming
However, traditional techniques for writing asynchronous applications can be complicated, making them difficult to write, debug, and maintain
Visual Studio 2012 introduces a simplified approach, async programming, that leverages asynchronous support in the .NET Framework 4.5 and the Windows Runtime
The compiler does the difficult work that the developer used to do, and your application retains a logical structure that resembles synchronous code
As a result, you get all the advantages of asynchronous programming with a fraction of the effort
Asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web
Access to a web resource sometimes is slow or delayed
If such an activity is blocked within a synchronous process, the entire application must wait
In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes
The Async and Await keywords are the heart of async programming
By using those two keywords, you can use resources in the .NET Framework or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method
Asynchronous methods that you define by using async and await are referred to as async methods
The InstallAsync method will return a value like total component installed
In async methods, you use the provided keywords and types to indicate what you want to do, and the compiler does the rest
including keeping track of what must happen when control returns to an await point in a suspended method
Some routine processes, such as loops and exception handling, can be difficult to handle in traditional asynchronous code
In an async method, you write these elements much as you would in a synchronous solution, and the problem is solved
Async methods are intended to be non-blocking operations
An await expression in an async method doesn't block the current thread while the awaited task is running
Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method
The async and await keywords don't cause additional threads to be created
Async methods don't require multithreading because an async method doesn't run on its own thread
The method runs on the current synchronization context and uses time on the thread only when the method is active
You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available
The async-based approach to asynchronous programming is preferable to existing approaches in almost every case
In particular, this approach is better than BackgroundWorker for IO-bound operations because the code is simpler and you don't have to guard against race conditions
In combination with Task.Run, async programming is better than BackgroundWorker for CPU-bound
operations because async programming separates the coordination details of running your code from the work that Task.Run transfers to the threadpool
We will debug the program
The most important thing to understand in asynchronous programming is how the control flow moves from method to method
Thank you for watching this video