Performance Optimization --. NET (C #) calls webService to obtain the region of the Client IP address (asynchronous callback) (2 ),
My friends shared this time that asynchronous callback is not an asynchronous call!
Please note!
Function Description,Interface address,Method NameAndParameter description. Same Article:. NET (C #) Call webService to obtain the region of the Client IP address (non-asynchronous) (1) (LZ is lazy and doesn't want to write too much! (⊙ 0 ⊙ ))
The implementation code is as follows:
1 namespace main 2 {3 class Program 4 {5 public static string Result = string. empty; 6 7 static void Main (string [] args) 8 {9 10 Stopwatch sw = Stopwatch. startNew (); 11 12 string strIP = "111.13.55.3"; // The requested IP address 13 14 Console. writeLine ("===== AsyncInvoke asynchronous callback start ===="); 15 16 GetCityIpHandler handler = new GetCityIpHandler (GetIp ); 17 18 /// its core lies in the implementation of the IAsyncResult interface 19 IAsyncResult res = handler. beginInv Ke (strIP, new AsyncCallback (GetIpSyn), string. Format ("===== main thread time {0} milliseconds! ==== ", Sw. elapsedMilliseconds); 20 21 Console. writeLine ("===== continue executing other tasks... ===== "); 22 23 Console. writeLine ("===== AsyncInvoke asynchronous callback is complete! = "); 24 25 sw. stop (); 26 27 Console. readLine (); 28 29} 30 public delegate string GetCityIpHandler (string strIp ); 31 /// <summary> 32 // Synchronization Method 33 /// </summary> 34 /// <param name = "strIP"> </param> 35 // /<returns> </returns> 36 public static string GetIp (string strIp) 37 {38 IpAddressSearchWebServiceSoapClient ws = new IpAddressSearchWebServiceSoapClient (); 39 Stopwatch sw = Stopwatch. startNew (); 40 string [] StrArea_IP = ws. getCountryCityByIp (strIp); 41 sw. stop (); // here we make a long call 42 return Result = string. format ("IP address attribution: {0}, query time: {1} milliseconds. ", strArea_IP [1], sw. elapsedMilliseconds ); 43} 44 // <summary> 45 // asynchronous callback method 46 // </summary> 47 // <param name = "result"> </param> 48 static void GetIpSyn (IAsyncResult result) 49 {50 GetCityIpHandler hl = (GetCityIpHandler) (AsyncResult) result ). asyncDelegate; // AsyncResult inherits from IAsy NcResult interface 51 Result = hl. endInvoke (result); 52 Console. writeLine (Result); // print the data returned by calling GetIp 53 object obj = result. asyncState; 54 Console. writeLine (obj); // print the returned result: a user-defined object that limits or contains information about asynchronous operations. 55} 56} 57}
The test results are as follows:
Okay, my friends. You have to go to bed. Be a healthy programmer. Good night! Pai_^
In addition, if you think this article is not bad, you can add it to your favorites. If you think LZ's blog is good, you can follow me or contact me for more support. Thank you! Good text to top