C # Programming Summary (VI) Asynchronous programming

Source: Internet
Author: User
Tags execution message queue thread web services

1, what is asynchronous?

Asynchronous operations are typically used to perform tasks that may take longer to complete, such as opening large files, connecting to a remote computer, or querying a database. Asynchronous operations are performed in a thread other than the main application thread. When an application invokes a method to perform an operation asynchronously, the application can continue execution while the asynchronous method performs its task.

2. The difference between synchronous and asynchronous

Synchronization (synchronous): When an operation is performed, the application must wait for the operation to complete before it can continue execution.

Asynchronous (asynchronous): When an operation is performed, the application can continue execution while the asynchronous operation is executing. Essence: Asynchronous operation, which starts a new thread, which executes in parallel with the method thread.

3, asynchronous and multithreading differences

We already know that the essence of asynchrony is the opening of new threads. What is the difference between it and multithreading?

Simply put: Asynchronous threads are managed by the thread pool, and multithreading allows us to control ourselves, and of course we can use thread pooling in multiple threads.

With the Internet, if you use asynchronous mode to implement, it uses the thread pool for management. When an asynchronous operation is executed, the operation is dropped to a worker thread in the thread pool to complete. When I/O is started, asynchronous will return the worker thread to the thread pool, which means that getting the Web page does not consume any CPU resources. It is not until the asynchronous completes that the page is finished, and asynchronously notifies the thread pool by callback. This shows that the asynchronous pattern, with the help of a thread pool, greatly saves CPU resources.

NOTE: DMA (direct Memory access) directly memory access, as the name suggests DMA function is to allow devices to bypass the processor, directly from memory to read data. Data exchange through direct memory access can almost without loss of CPU resources. In the hardware, the hard disk, network card, sound card, video card and so on have direct memory access function. The asynchronous programming model allows us to take full advantage of the direct memory access of the hardware to release the CPU pressure.

Scenarios for both:

Compute-intensive work, using multithreading.

IO-intensive work, using asynchronous mechanisms.

4. Asynchronous Application

Many aspects of the. NET Framework support asynchronous programming features, including:

1 file io, stream io, socket IO.

2) network.

3 Remote processing channel (HTTP, TCP) and proxy.

4 XML Web Services created using asp.net.

5) asp.net Web forms.

6 Use the MessageQueue class message queue.

The. NET Framework provides two design patterns for asynchronous operations:

1 asynchronous operation using the IAsyncResult object.

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.