Implement Asynchronous calling of Web Service to prevent page timeout

Source: Internet
Author: User

Generally, when the Web Service is executed, the client will wait until the Service is executed to respond. As a result, the UI thread of the client is blocked and suspended. At this time, asynchronous calling is very useful. It allows the client to call the Web Service without blocking the UI thread of the client, resulting in false positives, you can also perform other operations while calling the Web Service. There are also several different methods to call Web Service asynchronously. Here we will introduce two commonly used methods.

The first method is to use the Backgroundworker object.

The BackgroundWorker class allows you to run operations on individual dedicated threads. Time-consuming operations (such as downloads and database transactions) may cause the user interface (UI) to stop responding after a long time of running. You can use the BackgroundWorker class to solve the problem easily if you need a user interface that can respond and face long delays related to such operations.

Private void button#click (object sender, EventArgs e)

{

BackgroundWorker backgroundworker = new BackgroundWorker (); // register the method of asynchronous processing. doWork + = new DoWorkEventHandler (back_DoWork); // register the callback method backgroundworker after the call is completed. runWorkerCompleted + = new RunWorkerCompletedEventHandler (back_RunWorkerCompleted); // asynchronous call starts here

Backgroundworker. RunWorkerAsync (); // the client does not stop processing when the service is called.

ChangeProcessBar ();

} // Complete the event

Void back_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e)

{

If (e. Error! = Null) throw e. Error; progressBar1.Value = progressBar1.Maximum; // after the call is complete, the client progress bar is filled up

String price = e. Result. ToString (); // obtain the processing Result

MessageBox. Show ("call completed. Price: "+ price); // display the result value obtained from the server

} // Call Method

Void back_DoWork (object sender, DoWorkEventArgs e)

{// Web Service proxy class

ProductService. LTPService service = new ProductService. LTPService (); // call the Web method GetClass1 and assign the Result to the Result object of DoWorkEventArgs.

E. Result = service. GetProductPrice ("001 ");

} The ChangeProcessBar () method is used to process the code displayed in the progress bar control and represent other operations. //// Display the progress bar of the interface //

Void ChangeProcessBar ()

{

For (int I = 0; I <10; I ++)

{

ProgressBar1.Value = I; System. Threading. Thread. Sleep (500 );

}

}

The second method is to call the Async method implementation in the WebMethod of Web Service. After a Web Service reference is added, a proxy class is generated locally. There is a method with the same name as the original Web Service method and the suffix is Async.

Private void button2_Click (object sender, EventArgs e)

{// Web Service proxy class

ProductService. LTPService service = new ProductService. LTPService (); // call service. GetProductPriceAsync ("001") asynchronously. // register the callback method after the call is completed.

Service. GetProductPriceCompleted + = new ProductService. GetProductPriceCompletedEventHandler (GetProductPriceCompleted); // the client does not stop processing the call.

ChangeProcessBar ();

} // Event handling method

Void GetProductPriceCompleted (object sender, ProductService. GetProductPriceCompletedEventArgs e)

{

If (e. Error! = Null) throw e. Error; progressBar1.Value = progressBar1.Maximum; // after the call is complete, the client progress bar is filled up

String price = e. Result. ToString (); // obtain the processing Result

MessageBox. Show ("call completed. Price: "+ price); // display the result value obtained from the server

}

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.