C # advantages and methods of asynchronous calls

Source: Internet
Author: User

We need to clarify why asynchronous callback is required? As we all know, normal methods run in a single thread. Large operations (such as reading large files, operating databases in large batches, and network transmission) in the middle will lead to method blocking, as shown on the interface, Program Card or die, the interface element does not move, no response. The asynchronous method solves these problems. When a method is executed asynchronously, the program immediately opens up a new thread to run your method. The main thread, including the interface, will not die. Now we are talking about how to end this asynchronous new thread.
First, the asynchronous new thread must be recycled. It is a shameful waste of resources ,. net is also not allowed, so you do not want to explore the loopholes, as the saying goes, please be easy to god, it is this truth. Next, you can easily think of two types of recovery: active recovery and passive recovery (of course, this is my understanding, but Microsoft does not say so). Active recovery is, you need to monitor the thread and wait. When the Asynchronous Method is complete, the asynchronous thread will be recycled, and the focus will be returned to the main thread. This is actually the first article.ArticleIn C # asynchronous preliminary, endinvoke is followed by begininvoke. If the asynchronous thread does not complete the operation at endinvoke, the entire program, including the main thread, it is blocked again, and the interface will be "dead. To solve this problem, we should adopt the "passive reclaim" method. An important method is "Asynchronous callback ". Core: A, Use the callback function (callbackmethod in this example) ). This callback function is automatically called after the asynchronous process ends. B, In the preceding two examples, endinvoke is called in the main thread. This method calls endinvoke In the callback function. The general process of asynchronous callback is as follows: first, start Asynchronization. Then, the startup parameter is added with the method executed at the end of Asynchronization. Then, this asynchronous thread will be ignored, finally, when the asynchronous thread completes the work, it will automatically execute the method in the startup parameter, which is indeed very worry-free,CodeWriting is complicated. The code for souzang is as follows:

  1. // First, prepare the Asynchronous Method (asynchronous, preferably not multithreading)
  2. Privatestringmethodname (intnum, outintnum2)
  3. {
  4. Num2 = num;
  5. Return "helloworld ";
  6. }
  7. // Program endpoint
  8. // The method (callback method) to be executed when asynchronous completion is completed. This method can only have one iasyncresult parameter, but this parameter is almost omnipotent and can be passed to the object.
  9. Privatevoidcallbackmethod (iasyncresultar)
  10. {
  11. // Obtain the delegate object from the asynchronous ar. asyncstate.
  12. Delegatenamedn = (delegatename) Ar. asyncstate;
  13. // Output parameters
  14. Inti;
  15. // Be sure to use endinvoke. Otherwise, your end will be miserable.
  16. Stringr = DN. endinvoke (Outi, AR );
  17. MessageBox. Show ("Asynchronous completion! The value of I is "I. tostring ()", and the value of R is "R );
  18. }
  19. // Define the delegate with the same signature as the method
  20. Privatedelegatestringdelegatename (intnum, outintnum2 );
  21. // Program entry
  22. Privatevoidrun ()
  23. {
  24. // Instantiate the delegate and assign a value first
  25. Delegatenamedn = newdelegatename (methodname );
  26. // Output parameters
  27. Inti;
  28. // Instantiate the callback Method
  29. // Consider asynccallback as delegate. In fact, asynccallback is a Special Delegate, just like event.
  30. Asynccallbackacb = newasynccallback (callbackmethod );
  31. // Asynchronous Start
  32. // If the value of ACB is null, no callback method is available.
  33. // The last parameter dn can be replaced with any object. This object can be obtained by the callback method from the parameter and written as null. The parameter dn is equivalent to the thread ID. If multiple asynchronous threads exist, they can all be null, but they cannot be the same. They cannot be the same object. Otherwise, an exception occurs.
  34. Iasyncresultiar = DN. begininvoke (1, Outi, ACB, DN );
  35. // Do other things
  36. //............
  37. }
  38. // The final result should be: I = 1, R = "helloworld"
  39. // In addition, if you can, you can choose not to modify too much when defining the delegate:
  40. /// <Summary>
  41. /// Define the delegate
  42. /// </Summary>
  43. /// <Returns> </returns>
  44. Publicdelegateboolasyncdelegate ();
  45. /// <Summary>
  46. /// Callbackmethodmusthavethesamesignatureasthe
  47. /// Asynccallbackdelegate
  48. /// </Summary>
  49. /// <Paramname = "Ar"> </param>
  50. Privatevoidcallbackmethod (iasyncresultar)
  51. {
  52. // Retrievethedelegate.
  53. Asyncdelegatedlgt = (asyncdelegate) Ar. asyncstate;
  54. // Callendinvoketoretrievetheresults.
  55. Dlgt. endinvoke (AR );
  56. }
  57. // Call other methods:
  58. // Asynchronous execution
  59. // Specify the delegate Method
  60. Asyncdelegateisgt = newasyncdelegate (icpinfo. insert );
  61. Iasyncresultar = isgt. begininvoke (newasynccallback (callbackmethod), isgt );

 

 

To:Http://www.cnblogs.com/xiaoli0414/archive/2007/11/27/974534.html

 

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.