Multithreading asynchronous beginInvoke EndInvoke usage, begininvoke asynchronous

Source: Internet
Author: User

Multithreading asynchronous beginInvoke EndInvoke usage, begininvoke asynchronous

When there are many time-consuming operations, you need to respond to user operations. In this case, other threads or asynchronous threads are required. Originally, it was used to transform the company's log component. This is because the business of a foreign region has come to the system. Logs are good in other parts of the system, so they will be shown to us. Sometimes there is a piece of log missing when looking for a business process, which makes people want to die. At the end of the year, no business went online and decided to rebuild the log. The old man planted the tree and the old man had a cold heart. In the spirit of those who do not go to hell, I have finished this log. It is found that the processing of threads is not good in some places. By the way, I want to thank myself for the asynchronous or multithreading. Correct the error.

BeginInvoke EndInvoke usage

1. Usage history

 

These two methods were long ago and widely used in the. net Framework. For example, when referencing webservice, in addition to automatically generating synchronous methods, asynchronous calling methods will also be generated.

Different. net versions are directly encapsulated into beginXXXX and endXXXX. They are used to directly begin in and then process some things, and then end. However, there is a disadvantage that the fixed method is too dead.

Later, webservice encapsulated a commission with a complete suffix to end the Event Notification call. Then the calling method is AsycXXXX. It is similar to throwing events at the upper layer when writing controls by yourself.

There are some classes for IO and Network Programming to Implement this. For example, both read and write methods in NetworkStream are synchronous and asynchronous.

 

2. Usage

If you use these two methods, you can directly start in the same method to do something between them. However, this is too rigid. Generally, it is a delegated operation.

See the following code:

 

Public delegate bool deofdosomething (object param); public void invoketest () {Console. writeLine ("star .. "); // The passing parameter of the time-consuming operation object dosomeparam =" 1 "; // The execution of the time-consuming method ends. The object endinvokeparam =" 2 "can be obtained by the callback function "; deofdosomething de = new deofdosomething (dosomething); AsyncCallback acb = new AsyncCallback (invokecallback); IAsyncResult iar = de. beginInvoke (dosomeparam, acb, endinvokeparam); if (iar. asyncWaitHandle. waitOne (1000) {Console. writeLine ("not time out");} else {Console. writeLine ("time out") ;}} private bool dosomething (object obj) {// time-consuming operation Thread. sleep (2000); // dosomeparam Console. writeLine (obj. toString (); return true ;}

 

 

 

AsyncCallback is a special delegate in the. net Framework. It can be used to notify you if the execution is completed after an asynchronous call. Of course, null can also be input. It indicates that the operation is not based on the result.

Iar. AsyncWaitHandle. WaitOne is the statement handle after begin. It can be used to set whether to wait for asynchronous timeout. Same as autoresetevent, if the time-out period is exceeded, return immediately. I suspect it is implemented using this tool.
A boolean value is returned. If the value does not time out, the value is TRUE. If the value times out, the value is FALSE.
 
Private void invokecallback (IAsyncResult iar) {deofdosomething de = (deofdosomething) (AsyncResult) iar ). asyncDelegate; // endinvokeparam string targeparam = (AsyncResult) iar ). asyncState. toString (); Console. writeLine (targeparam); // dosomething returns bool issuccess = de. endInvoke (iar); Console. writeLine ("end ");}

 

IAsyncResult is the method for transmitting and executing the delegation completed by asynchronous operations. As an interface, you can directly convert it to AsyncResult. Its AsyncDelegate can obtain the delegate that references it.

AsyncState is an object-type parameter, which is a parameter sent from begin, because asynchronous operations can bring some additional information.

Here is the test result. Someone will bother me. Wait for another day to write it. It is expected that there are two other tasks in the threadpool. Hope to stick to it.

 

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.