C # Advanced Programming, 11th Edition - Chapter XV

navigation

15 Asynchronous Programming

15.1 The importance of asynchronous programming

.NET Framework4.5 new parallel library (Task Parallel Library, TPL), making .NET parallel programming more simple. C # 5.0 adds two new keywords: async and await, making asynchronous programming is no longer a difficult task. This chapter focuses on the use of async and await.

In asynchronous programming, a method call is running in the background (often through the help of a task or thread), and the thread where the caller will not be suspended or blocked (not blocked).

In this chapter, you can understand the difference between different modes, such as asynchronous mode (asynchronous pattern), an event-based asynchronous pattern (event-based asynchronous pattern), based on asynchronous mode task (task-based asynchronous pattern, TAP). Uses async and await in TAP. When you compare these different modes, asynchronous programming you will find a real advantage.

After having discussed different models, you will by creating tasks and asynchronous method calls, to learn basic asynchronous programming components. You'll learn those hidden between subsequent task (continuation tasks) and asynchronous contextual details.

It is important to emphasize that Error processing; because in different asynchronous tasks, the same scenario requires a number of different error handling.

The last part of this chapter will discuss Universal Windows App scenarios in asynchronous programming is and what needs attention.

Note: Tasks and parallel programming are covered in Chapter 21.

When your application is not responding at every turn, users will find it very annoying. Although when using the mouse, we're used to it there is a little delay, because we have been using it for many years. However touch UI, an App immediate need to respond to the request. Otherwise, users will try to touch back and forth (redo the action).

Because asynchronous programming is difficult to achieve in the old version of the .NET Framework, so it is often not achieve the desired goal. In older versions of Visual Studio, often you have an application that can block the UI thread. In that case, if you want to open a solution that has hundreds of items it will take a considerable amount of time (take a long coffee break). VS2017 provides a lightweight solution for load characteristics, only when the project will need to be used to load, and that the selected projects will be given priority loaded. VS2015 from the beginning, NuGet package manager is no longer implemented as a dialog form. The new package manager can NuGet when you perform other operations, asynchronous loading package information. These are just Visual Studio in some important examples on asynchronous programming.

Many .NET API provides synchronous and asynchronous versions. Because the synchronous version of the method easier to use, so it is often called in the wrong place. When running through the new Windows (Windows Runtime, WinRT), if an API call more than 40 milliseconds, only its asynchronous version is valid. Starting from the C # 5.0, asynchronous programming became more and more easy, just as synchronization, so the use of asynchronous API does not require any burden. Of course, there are many asynchronous API in pits (traps), this chapter will be introduced one by one.

15.2 asynchronous programming in .NET history

Before you start using the new async and await keywords, it is best to first understand what the .NET Framework asynchronous programming model once. Early in the .NET Framework 1.0, when you already have the characteristics asynchronous and many of the .NET Framework base class support more than one asynchronous programming model.

Here, we mainly perform network requests a synchronous (synchronous networking call) the following three ways:

  • Asynchronous mode (asynchronous pattern)
  • Event-based asynchronous mode (event-based asynchronous pattern)
  • Based Asynchronous Pattern task (task-based asynchronous pattern, TAP)

Asynchronous mode is the first asynchronous processing features, not only to support a range of API, also supports the basic functions, such as delegate type.

Because asynchronous mode is used to update the UI is very complicated - either through Windows Forms or WPF, .NET 2.0 it is recommended to start the event-based asynchronous mode. In this mode, the control of thread synchronization context calls an event handler (event handler), so the UI will update this mode is very simple. Let me talk about, this model is also known as asynchronous component mode (asynchronous component pattern).

By .NET Framework 4.5, you can use asynchronous programming in another way, TAP. This model is based Task type, by using the compiler features async and await keywords, and to support asynchronous processing.

HistorySample sample code above uses the characteristics of at least 7.1 and C # namespaces those listed below:

  • System
  • System.IO
  • System.Net
  • System.Threading.Tasks

15.2.1 synchronous call

313 15.2.2 Asynchronous mode

15.2.3 Event-based Asynchronous Pattern 314

15.2.4 Task-based Asynchronous Pattern 314

15.2.5 Induction Main () method 315

Basis 15.3 asynchronous programming 315

15.3.1 Creating a Task 316

15.3.2 asynchronous method call 316

15.3.3 Use Awaiter 317

15.3.4 continuation of the task 317

15.3.5 synchronization context 318

15.3.6 318 using a plurality of asynchronous methods

15.3.7 Use ValueTasks 319

15.3.8 convert asynchronous mode 320

15.4 Error Handling 320

Exception handling asynchronous method 15.4.1 321

Exception handling over 321 15.4.2 asynchronous method

15.4.3 use 322 information AggregateException

15.5 Asynchronous with Windows applications 322

15.5.1 Configuration await 323

15.5.2 switch to the UI thread 324

15.5.3 Use IAsyncOperation 325

15.5.4 avoid obstruction 325

15.6 Summary 325

Guess you like

Origin www.cnblogs.com/zenronphy/p/12501609.html