Synchronous WebService call and asynchronous WebService call

Source: Internet
Author: User

Reading directory

I. synchronous call

Ii. asynchronous call

Iii. synchronous WebService call

Iv. asynchronous WebService call

 I. synchronous call

    A synchronization operation will block the entire process until the operation is completed before the next code segment can be executed.

  

Ii. asynchronous call

    The call thread of the startup operation is not blocked. The caller must take turns to detect the call or wait for the Completion signal to find the completion of the call.

  

Iii. synchronous WebService call

    Synchronous WebService call is our usual method of calling

      1: AsynCall. asmx's AsynCall. cs code

1 /// <summary> 2 /// obtain the score of the student based on the student name. 3 /// </summary> 4 /// <param name = "strName"> Student name </param> 5 /// <param name = "intDelaySecond"> Number of seconds delayed </param> 6 /// <returns> score </returns> 7 [WebMethod] 8 public int GetGrade (string strName, int intDelaySecond) 9 {10 if (intDelaySecond> 0) 11 {12 Thread. sleep (intDelaySecond * 1000); 13} 14 int intGrade = 0; 15 switch (strName) 16 {17 case "Zhang San": 18 intGrade = 90; 19 break; 20 case "Li Si": 21 intGrade = 80; 22 break; 23 case "Wang Wu": 24 intGrade = 70; 25 break; 26 case "Jia 6": 27 intGrade = 60; 28 break; 29} 30 return intGrade; 31}

  2: Program. cs

1 AsynCall. asynCall service = new AsynCall. asynCall (); 2 Console. writeLine ("enter the name of the student whose score you want to query:"); 3 string strName = Console. readLine (); 4 Console. writeLine ("Enter the number of seconds to be delayed:"); 5 int intSecond = int. parse (Console. readLine (); 6 7 Console. writeLine ("synchronous call start"); 8 Console. writeLine ("synchronous call Result:" + service. getGrade (strName, intSecond); 9 Console. writeLine ("End of synchronous call"); 10 11 Console. readLine ();

Enter 30 seconds to check the running effect.

      

Because we entered 30 seconds, we had to wait 30 seconds before outputting the "synchronous call result: 90" and "synchronous call ended" statements.

      

Iv. asynchronous WebService call

.. NET Framework provides necessary services for asynchronous programming models

.It is determined by the client or the caller to determine whether a specific call should be asynchronously processed. We can see that the WebService itself has not changed. What changes are the client and the call end.

    .No additional programming required

    .The proxy class provides the WebService method name + Async () as the method name for asynchronous WebService call.

    .The essence of asynchronous calling is the process of concurrent execution between the caller thread and the called thread.

1 Console. writeLine ("Asynchronous call start"); 2 3 // '+ =' indicates the event reservation, 'new AsynCall. getGradeCompletedEventHandler (service_Completed) 'indicates that a delegate instance is generated using the written event handler, 'service. getGradeCompleted indicates the added event to the event list of the event object. The combined explanation is to add the delegated instance to the event list of the event object. This process is called subscription event, the delegate is the proxy. We can understand that the delegate is to associate an event processing function with an event. 4 service. getGradeCompleted + = new AsynCall. getGradeCompletedEventHandler (service_Completed); 5 // call 6 service asynchronously. getGradeAsync (strName, intSecond );
7. Console. writeLine ("the current process is not blocked"); 8 for (int I = 0; I <3; I ++) 9 {10 Console. writeLine (I); 11} 12 Console. readLine (); 13 14 private static void service_Completed (object sender, AsynCall. getGradeCompletedEventArgs e) 15 {16 Console. writeLine ("Asynchronous call Result:" + e. result); 17 Console. writeLine ("End of asynchronous call"); 18}

    Let's enter 30 seconds to check the running effect.

    

Because the input is still 30 seconds, we have to wait 30 seconds before outputting the "Asynchronous call result: 90" sentence. No one can change this, however, unlike synchronous calls, the "the current process is not blocked" and the for loop is output immediately, which proves that when you delay for 30 seconds, I will continue to execute the code below. You will not care about your delay. I will not wait for you. When you are in the Year of the Monkey, I will continue to do my work.

 

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.