What friends are sharing this time is that asynchronous callbacks are not asynchronous calls Oh!
Please pay attention!
function Description , interface address , method name and parameter description , same as:. NET (C #) Call WebService get client IP address zone (non-async) (one) (LZ lazy, Don't want to write too much Oh! (⊙0⊙))
The implementation code is as follows:
1 namespaceMain2 {3 class Program4 {5 Public Static stringResult =string. Empty;6 7 Static voidMain (string[] args)8 {9 TenStopwatch SW =stopwatch.startnew (); One A stringStrIP ="111.13.55.3";//the requested IP address - -Console.WriteLine ("===== Asyncinvoke Asynchronous callback starts ====="); the -Getcityiphandler handler =NewGetcityiphandler (getip); - - The core of////is the implementation of the IAsyncResult interface. +IAsyncResult res = handler. BeginInvoke (StrIP,NewAsyncCallback (Getipsyn),string. Format ("===== Main thread time is {0} milliseconds! =====", SW. Elapsedmilliseconds)); - +Console.WriteLine ("===== continue to do other things ... ====="); A atConsole.WriteLine ("===== Asyncinvoke Asynchronous callback complete! ====="); - - SW. Stop (); - - console.readline (); - in } - Public Delegate stringGetcityiphandler (stringstrIp); to /// <summary> + ///Synchronization Method - /// </summary> the /// <param name= "StrIP" ></param> * /// <returns></returns> $ Public Static stringGetIP (stringstrIp)Panax Notoginseng { -Ipaddresssearchwebservicesoapclient ws =Newipaddresssearchwebservicesoapclient (); theStopwatch SW =stopwatch.startnew (); + string[] Strarea_ip =Ws.getcountrycitybyip (strIp); A SW. Stop ();//Here we make a lengthy call the returnResult =string. Format ("The IP attribution is {0}, and the query time is {1} milliseconds.", strarea_ip[1], SW. Elapsedmilliseconds); + } - /// <summary> $ ///Asynchronous Callback methods $ /// </summary> - /// <param name= "result" ></param> - Static voidGetipsyn (IAsyncResult result) the { -Getcityiphandler hl = (Getcityiphandler) ((AsyncResult) result). AsyncDelegate;//AsyncResult inherit from IAsyncResult interfaceWuyiResult =hl. EndInvoke (result); theConsole.WriteLine (Result);//print the data returned by the call GetIP - Objectobj =result. asyncstate; WuConsole.WriteLine (obj);//Print return Result: A user-defined object that qualifies or contains information about an asynchronous operation. - } About } $}
The test results are as follows:
All right, friends, get some sleep, be a healthy programmer, good night! ^_^
Add a sentence, if you think this article is good, you can collect the article if you think LZ blog is also good words can follow me or contact me please support, thank you! Good text to the top
Performance optimization ——. NET (C #) Call WebService get the IP address of the client zone (asynchronous callback) (ii)