Asynchronous programming mode (APM) in. Net (2)

Source: Internet
Author: User
Tags apm
ArticleDirectory
    • Why do you need to implement APM on your own?
    • Asyncresult and asyncresult in the powerthreading class library <t>

Note: This article is mainly about the implementing the CLR asynchronous programming model of Jeffery Richard. You can skip this article.

The previous article introduced the use of APM to develop multithreading.ProgramThere are also a lot of articles about how to use APM. Therefore, this article mainly wants to see how to use the power threading class library of Jeffery Richard to develop a class that supports APM.

Why do you need to implement APM on your own?

In the previous article, we mentioned the difference between computing-bound and I/O constraints. If we are a computing-constrained task, we can convert this method into a delegate form, and then implement APM through begininvoke/endinvoke. In addition, FCL also encapsulates a large number of classes for asynchronous I/O operations, such as filestream and networkstream. Why do we need to implement APM on our own. Jeffery Richard lists four reasons:

    1. Although FCL provides many asynchronous classes for communication with devices, some of them are not provided, such as parallel interfaces, or FCL does not provide enough functions.
    2. Add functions to the existing I/O class library. For example, we can develop an HTTP filter class in webhttprequest.
    3. Implement computing-constrained APM
    4. Provides asynchronous access for devices that do not support asynchronous mode
Asyncresult and asyncresult in the powerthreading class library <t>

The core of APM is the iasyncresult interface. When beginxxx is called asynchronously, The iasyncresult object is returned. To return objects that meet this interface, our class needs to maintain an iscompleted status, a manuelresetevent object, and a delegate persistence callback function. These are simple, but they are very troublesome. Using asyncresult and asyncresult <t> In the powerthreading class library, we can easily implement classes that support APM.

Asyncresult is used for tasks with no return value, while asyncresult <t> is used for tasks with a return type of T. The following example shows how to use asyncresult <t>.

First, we are going to implement a class that can filter stream objects. The class is defined as follows:

1 Class Streamfilter
2 {
3 Private Stream m_stream;
4 Public Streamfilter (Stream)
5 {
6 }
7
8 Public Iasyncresult beginparse (
9 Asynccallback callback, object state)
10 {
11 }
12
13 Public Stream parse ()
14 {
15 }
16
17 Public Stream endparse (iasyncresult asyncresult)
18 {
19 }
20 }

Parse () is the synchronous access interface that executes the main tasks, while beginparse () and endparse () are the corresponding asynchronous interfaces. The following isCode

1 Class Streamfilter
2 {
3 Private Stream m_stream;
4 Public Streamfilter (Stream)
5 {
6 M_stream = Stream;
7 }
8
9 Public Iasyncresult beginparse (
10 Asynccallback callback, object state)
11 {
12 // Create an iasyncresult object to mark asynchronous operations
13 Asyncresult < Stream > Ar =  
14 New Asyncresult < Stream > (Callback, State );
15
16 // Call the auxiliary function and pass the asycnresult object
17 // If you use anonymouse method
18 Threadpool. queueuserworkitem (parsehelper, AR );
19
20 Return Ar; // Returned iasyncresult
21 }
22
23 Private   Void Parsehelper (Object asyncresult)
24 {
25 VaR ar = (Asyncresult < Stream > ) Asyncresult;
26 Try
27 {
28 // Execute real tasks
29 Stream result = Parse ();
30
31 // Update asycnresult status
32 Ar. setascompleted (result, False );
33 } Catch (Exception E)
34 {
35 // Save exception object
36 Ar. setascompleted (E, False );
37 }
38 }
39
40 Public Stream parse ()
41 {
42 // Analyze m_stream and return the filtered Stream Object
43 Stream filterstream =   Null ;
44 Return Filterstream;
45 }
46
47 Public Stream endparse (iasyncresult asyncresult)
48 {
49 VaR ar = (Asyncresult < Stream > ) Asyncresult;
50 // Wait until the task is completed
51 Return Ar. endinvoke ();
52 }
53 }

 

More importantly, the parsehelper function, which is executed in a new thread and updated through ar. setascompleted. If you combine anonymous methods or limbda expressions, the code can become more compact.

 

Refer:

Implementing the CLR asynchronous programming model by Jeffery Richard

Power threading class library

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.