Asynchronous Web Service call

Original link: http://www.cnblogs.com/jvstudio/archive/2009/11/23/1608628.html

Ordinary call the Web Service method, the client during the execution of Web Service will wait for the response to the service is finished. Thereby causing obstruction client UI thread phenomenon of suspended animation. At this time, it is useful to an asynchronous call, which allows the client when invoking the Web Service, and will not clog the client UI thread leads to suspended animation, you can also do other processing while calling the Web Service. Web Service asynchronous call there are several different ways to introduce two common here.

The first method is achieved by using Backgroundworker object. BackgroundWorker class allows you to run on a separate, dedicated thread operations. Time-consuming operations (such as downloads and database transactions) in the long run may cause the user interface (UI) appears to stop responding in the state. If you need to be able to respond to the user interface, but also faced with this type of operation associated with the long delay, you can use the BackgroundWorker class to easily solve the problem.

 

 
  
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     BackgroundWorker backgroundworker = new BackgroundWorker();  
  4.     // Register the specific method of asynchronous processing  
  5.     backgroundworker.DoWork += new DoWorkEventHandler(back_DoWork);  
  6.     // register a callback method after the call is completed  
  7.     backgroundworker.RunWorkerCompleted +=   
  8.         new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);  
  9.     // here to start an asynchronous call  
  10.     backgroundworker.RunWorkerAsync();  
  11.     // call while client-side processing services do not stop  
  12.     ChangeProcessBar();   
  13. }  
  14. // completion event  
  15. void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
  16. {  
  17.     if (e.Error != null)  
  18.         throw e.Error;  
  19.     = progressBar1.Maximum progressBar1.Value;  // call is complete, the client progress bar filled up 
  20.     String  . price e.Result.ToString = ();    // Get processing result 
  21.     MessageBox.Show ( "call completion prices are:."  +. Price);  // display the results obtained from the server value 
  22. }  
  23. // call the method  
  24. void back_DoWork(object sender, DoWorkEventArgs e)  
  25. {  
  26.     // Web Service proxy class  
  27.     ProductService.LTPService service = new ProductService.LTPService();  
  28.     // Call Web methods GetClass1, the results of Result objects assigned to DoWorkEventArgs  
  29.     e.Result = service.GetProductPrice("001");   

Wherein, ChangeProcessBar () method is the progress bar display control code to represent other operations.

 
  
  1. //<summary>  
  2. // display the progress bar interface  
  3. //</summary>  
  4. void ChangeProcessBar()  
  5. {  
  6.     for (int i = 0; i < 10; i++)  
  7.     {  
  8.         progressBar1.Value = i;  
  9.         System.Threading.Thread.Sleep(500);  
  10.     }  

The second method is a method Async invoke the Web Service WebMethod in implementation. When you are finished adding reference to the Web Service after the proxy class is generated locally, which will have a same and original Web Service method name with the suffix Async method.

(Example here: CD \ code \ ch07 \ WindowsFormsApplication1)

 
  
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     // Web Service proxy class  
  4.     ProductService.LTPService service = new ProductService.LTPService();  
  5.     // here to start an asynchronous call  
  6.     service.GetProductPriceAsync("001");   
  7.     // register a callback method after the call is completed  
  8.     service.GetProductPriceCompleted += new ProductService.  
  9.         GetProductPriceCompletedEventHandler(GetProductPriceCompleted);  
  10.     // call and the client process does not stop    
  11.     ChangeProcessBar();  
  12. }  
  13. // complete event handler method  
  14. void GetProductPriceCompleted(object sender, ProductService.  
  15.             GetProductPriceCompletedEventArgs e)  
  16. {  
  17.     if (e.Error != null)  
  18.         throw e.Error;  
  19.     = progressBar1.Maximum progressBar1.Value;   // call is complete, the client progress bar filled up 
  20.     String  . price e.Result.ToString = ();          // Get processing result 
  21.     MessageBox.Show ( "call completion prices are:."  +. Price);    // display the results obtained from the server value 

Address reprint: http://book.51cto.com/art/200906/129768.htm 

 

Reproduced in: https: //www.cnblogs.com/jvstudio/archive/2009/11/23/1608628.html

Guess you like

Origin blog.csdn.net/weixin_30194145/article/details/94798282