APM (asynchronous programming model)

Source: Internet
Author: User
Tags apm
Recently, there is a function that requires statistics on a page. The data is distributed in different databases of three servers, and the page is frequently accessed, I processed it in a common way and found that the speed was slow. I had to think of other methods to solve it. I remembered the previously written ADO. net new features: Mars (multiple active result set) and asynchronous Command Execution Article In this case, I think it is a good idea to use Asynchronous processing. After testing, I found that the speed is indeed much faster. If it is to modify the class library and add the asynchronous execution method, in addition, I decided to modify the log class library and add the relevant asynchronous execution methods, and write log files and send emails to execute them asynchronously.
. Net Framework 2.0 adds many asynchronous execution methods. sqlcommad adds beginexecutenonquery, endexecutenonquery, beginexecutereader, endexecutereader, beginexecutexmlreader, endexecutexmlreader, and stream class, related asynchronous execution methods are also added to the webrequest class. All these methods start with "end" and return an iasyncresult object, which has an asyncstate attribute, this attribute is very important because it can be used to pass any object we want to pass. These are actually asynchronous programming models (APM -- asynchronous programming model ).
APM supports the following methods: Wait-until-done, polling, and method callback.
However, after calling a series of beginxx methods, these methods will request the operation queue and then return the iasyncresult object to identify the pending operation. If the asynchronous operation is completed, the returned result can be obtained when the endxx method is called.
Wait-until-done (wait till completion): If asynchronous operations are not completed, the endxx method suspends the calling thread.
Polling: to implement polling, a thread regularly queries CLR to obtain the execution status of asynchronous requests. In this case, the asyncwaithandle and iscompleted attributes of the iasyncresult interface come in handy.
Method callback: Call the specified callback method immediately after an asynchronous request is executed.
I think the callback method is the most efficient, because using this method will not put a thread in the waiting state (wait-until-done ), it does not allow a thread to regularly ask the CLR to obtain the asynchronous request execution status.

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.