C # Review notes (5)--C#5: Simplified Asynchronous programming (basic knowledge of asynchronous programming)

Source: Internet
Author: User

basic knowledge of asynchronous programming

The Async and await keywords introduced by c#5 make asynchronous programming a lot easier on the surface, and we only need to know a little knowledge to write valid asynchronous code.

Before introducing async and await, let's introduce some basic concepts:

Concurrency: Do a lot of things at the same time.

This explanation directly indicates the effect of concurrency. The end-user program takes advantage of concurrency to respond to user input while entering a database. The server application takes advantage of concurrency to respond to the second request while processing the first request. As long as you want the program to do multiple things at the same time, you need concurrency. Almost every software program will benefit from concurrency.

Multithreading: A form of concurrency that takes multiple threads to execute a program.

In the literal sense, multithreading is the use of multiple threads. The following chapters of this book will show that multithreading is a form of concurrency, but not the only form. In fact, the use of the underlying thread type directly does not work in modern programs. Using a high-level abstraction mechanism makes the program more powerful and more efficient than the old-fashioned multithreading mechanism. Therefore, this book will try not to involve some outdated technology. All multithreaded methods in the book are advanced types, not thread or BackgroundWorker. However, do not think that multithreading has been completely eliminated! Because the thread pool requires that multithreading continue to exist. The thread pool holds the queue for the task, which can be adjusted to suit its needs. Correspondingly, the thread pool produces another important concurrency form: parallel processing.

Parallel processing: A large number of tasks that are being executed are split into small chunks that are allocated to multiple concurrently running threads.

In order to maximize the efficiency of processor utilization, parallel processing (or parallel programming) uses multithreading. When modern multi-core CPUs perform a large number of tasks, it is clearly unreasonable to perform all tasks with one core, while others remain idle. Parallel processing divides the tasks into small chunks and assigns them to multiple threads, allowing them to run independently on different cores. Parallel processing is one of multithreading, and multithreading is a kind of concurrency. In modern programs, there is also a very important concurrency type that many people are unfamiliar with: asynchronous programming.

Asynchronous programming: A form of concurrency that uses the future mode or callback (callback) mechanism to avoid generating unnecessary threads.

A future (or promise) type represents some of the operations that are about to be completed. In. NET, the new future type has task and task<tresult>. In the old asynchronous programming API, callbacks or events are used instead of the future. The core idea of asynchronous programming is the asynchronous operation: the initiated operation will be completed after a certain period of time. When this operation is executing, the original thread is not blocked. The thread that initiated the operation can continue to perform other tasks. When the operation completes, it notifies the future of it, or invokes a callback function to let the program know that the operation has ended. Asynchronous programming is a powerful concurrency form, but until recently, the implementation of asynchronous programming still requires particularly complex code. VS2012 supports async and await, which makes asynchronous programming almost as easy as synchronous (non-concurrent) programming.

Introduction to asynchronous programming the execution flow of asynchronous programming

Modern, asynchronous. NET program uses two keywords: async and await. The Async keyword is added to the method declaration, and its main purpose is to make the await keyword in the method take effect (in order to maintain backward compatibility and introduce these two keywords). If the Async method has a return value, it should return task<t>; if there is no return value, the task should be returned. These task types are equivalent to the future and are used to notify the main program at the end of an asynchronous method. There is also a return void, which is the same as the memory for a compatible event handler. Therefore, in addition to registering a practice handler, it is not recommended to use asynchronous code that returns void elsewhere.

Let's look at an example:

Async Task Asyncmethod ()        {            Console.WriteLine ("sync execute before await");//① code             executed synchronously Await Task.delay (Timespan.fromseconds (5)),//② asynchronous wait, non-blocking            Console.WriteLine ("  callback method");//③ the continuation of the task        }

As with other methods, the Async method executes the ① synchronously at the beginning. Inside the Async method, the await keyword performs an asynchronous wait ② on its parameters. It first checks whether the operation has completed and, if it is done, continues to run (synchronous mode). Otherwise, it pauses the Async method and returns, leaving an unfinished task (token). After some time, the operation is completed, and the Async method resumes running ③. It's that simple. The compiler helps us do a lot of work later on. In the subsequent chapters, the compiler's actions are described in detail.

Synchronization Context: An async method is made up of multiple synchronously executed blocks of ②, separated by an await statement between each Synchronizer block. The first Synchronizer block runs in the thread that calls this method, but where does the other Synchronizer block run? The situation is more complicated. The most common scenario is to wait for a task to complete with an await statement, and when the method pauses at an await, the context is captured. If the current SynchronizationContext is not empty, this context is the current SynchronizationContext. If the current SynchronizationContext is empty, then this context is the current TaskScheduler. The method will continue to run ③ in this context. In general, the UI context is used when the UI thread is running, and the ASP. NET request context is used when processing the ASP, and in many other cases the thread pool context is used. Therefore, in the preceding code, each Synchronizer block tries to resume running in the original context. If you call Dosomethingasync in the UI thread, each of the Synchronizer blocks of this method will run on this UI thread. However, if the thread pool threads are called, each Synchronizer block will run on the thread pool threads. To avoid this behavior, you can use the Configureawait method in await to set the parameter Continueoncapturedcontext to False. The next code is just beginning to run in the calling thread, and after the await is paused, the thread pool line thread will continue to run.

wait mode: The keyword await can be used not only for a task, but for all awaitable types that follow a particular pattern--in a compiler-generated state machine, there is a MoveNext method, In this method, the await object is called whether there is a Getawaiter method, whether the method returns whether a awaiter,awaiter contains the GetResult method and the IsCompleted property, Awaiter follow the interface is Inotifycompletion and Icriticalnotifycompletion, from the name above to know that they mean to send a notification. One feature of similar awaitable types, yieldawaitable and configuredtaskawaitable,awaitable types, is that there is a Getawaiter method to return a awaiter.

Iterators are similar in principle, and it appears earlier (C#2). When the Foreach loop is a sequence, he does not require that the sequence be implemented IEnumerable or Ienumerable<t>,foreach will find out if the sequence has a GetEnumerator method, Whether this method returns a enumerator, whether the enumerator contains a MoveNext method and a current property that returns the present element.

Handling Exceptions

Because the task's execution thread is not determined, the task does not actively throw an exception, which indicates the execution result of the task on the Status property in the returned task, and the Status property is the TaskStatus enumeration type, defined as follows:

 Public enum TaskStatus        {            Created,            waitingforactivation,            waitingtorun,            Running,            waitingforchildrentocomplete,            Rantocompletion,            Canceled,            Faulted,        }

There is also a exception property on the task, and the type is a aggregateexception. Normally, exception returns null when the task execution completes successfully. When a task execution fails, the Exception property returns an exception of type aggregateexception, which contains all the exceptions thrown during the execution of the task. When an exception occurs, the task ends without throwing an exception directly. Only when using a Task, such as task.wait (), Task.whenall (), and so on. Also, when a task is await, an exception is thrown, but the first exception in the AggregateException is thrown.

Dead lock

Another important guideline for asynchronous programming is that, where there is a UI thread, if you are going to use asynchronous programming, you need to get to the bottom of the line and consider the following code:

 async   Task Waitasync () {            Span style= "COLOR: #008000" >//  Here Awati will capture the current context ...  await  Task.delay (Timespan.fromseconds (1 )); //  ... This will attempt to continue with the context captured above  }  void  
   
     Deadlock () { 
    // 
     Start delay  Task task = Waitasync (); 
    // 
     synchronization block, waiting for async method to complete  
     task.        Wait (); }
   

If the above code executes in the UI thread or ASP., a deadlock will occur to see what happens: both contexts can only run one thread at a time. The deadlock method calls the Waitasync method, and the Waitasync method starts calling the delay statement. Then, the deadlock method (synchronous) waits for the Waitasync method to complete while blocking the context thread. When the delay statement ends, await attempts to continue running the Waitasync method in the captured context, but this step cannot succeed because there is already a blocked thread in the context, and this context allows only one thread to run at the same time. There are two ways to avoid deadlocks: using Configureawait (False) in Waitasync (causing await to ignore the context of the method), or calling the Waitasync method with an await statement (making deadlock an asynchronous method).

There are so many basic things, there are not many examples in this chapter, but if you can read all the meanings inside, then writing asynchronous programming is not a difficult task. The following sections describe in detail everything the compiler does for async and await, and will extend the relevant concepts further.

C # Review notes (5)--C#5: Simplified Asynchronous programming (basic knowledge of asynchronous programming)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.