. Net-C # asynchronous program knowledge point sorting,

Source: Internet
Author: User

. Net-C # asynchronous program knowledge point sorting,

  • Thread:
    Each Windows process has a Main Thread used to enter the entry point of the program, for example. the Main () method is used as the entry point of the program. When you call this method, the Main thread is automatically created.
    A thread is an independent execution unit in a Windows Process. Each thread has a main thread (established at the execution entry point) and each thread can create a new thread in the program mode.
    Thread allocation and startup require a certain amount of time and cost, so this process is not real-time.

  • Task class:
    Compared with Thread, Task is a more abstract concept. MSDN provides a simple description: "asynchronous operations", that is, asynchronous operations that can be executed in multiple threads, starting from net framework4.5, the simplest way to implement the background thread of a Task is to use the static method of Task. run (), in. net Framework 4.0 can call Task. factory. startNew method to achieve the same effect

  • Continuous work tells the Task to continue executing subsequent jobs after completion. Continuous work is usually implemented by a callback method, which will be executed once after the Task is completed. Add continuous work to a Task in two ways,

  1. The first method is to call Task. ContinueWith.
  2. The second method is the Awaiter method added by. Net Framework4.5. It is very important because the asynchronous function of C #5.0 uses this method. Check the program code first.
Task <int> task = Task. run () => Enumerable. range ). count (n => (n % 3) = 0); var awaiter = task. getAwaiter (); awaiter. onCompleted () => {int result = awaiter. getResult (); Console. writeLine ("the number of Division 3 is:" + Result); Console. writeLine ("the Task is being executed... ") Console. readLine ();});

Call Task. the GetAwaiter method returns an awaiter (waiting for) object, which allows the previous Task to execute a delegate after completion or error. If an error occurs in the previous Task, awaited is called when the connection is completed. an exception is displayed when GetResult () is used. The advantage of using GetResult is that when the previous Task fails, exceptions can be directly popped up without being encapsulated in aggresponexception.

  • The Task. Delay method is the asynchronous version of Thread. Sleep.
  • The Parallel class mainly provides support For Parallel Loops and regions. It mainly includes three methods: For, Foreach, and Invoke. It mainly describes the Invoke method, which simplifies the start of a group of Parallel operations, it accepts an array of actions.
  • Async and await keywords have async modifiers, not necessarily await operators, but await operators must have async modifiers. According to Microsoft's suggestion, the Asynchronous Method compiled by developers should end with "Async.
  • Asynchronization and thread pool
    When using multithreading technology in applications, you must pay attention to it first .. Net framework maintains a thread pool in IIS. When iis receives a request, it reads a thread-processed request from the thread pool. If synchronous processing is used to process the request, this thread will always serve its own requests until the program completes processing, and this thread cannot serve other requests, which is called a blocking. If the thread pool is large enough, there is no problem, but the number of thread pools is limited. When there are a large number of sudden requests, or the request thread can be busy with backend high-latency network tasks, all threads in the thread pool are blocked, which is called thread depletion. When the thread is exhausted, IIS will start to queue the request. If the queue is full, IIS will reject the request and display the http503 status. If the asynchronous application is normal, however, the http503 status may occasionally occur, so the course class can be started. If the accident is near the county seat, you can try to increase the queue length, for example, to one thousand.

 

Note: The content is based on the book "The beauty of ASP. Net MVC5 website development ".

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.