Performance optimization ——. NET (C #) Call WebService get the IP address of the client zone (asynchronous callback) (ii)

Source: Internet
Author: User

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)

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.