异步调用webservice

通常的调用webservice方法,在webservice执行期间客户端会一直等待服务执行完毕才能相应,直接影响用户体验。

在webservice中定义一个方法,比如Test(),当调用这webservice时候,会发现不仅有Test()方法,还有一个TestAsync()。

异步
 1  public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             MyService.WebService service = new WindowsApplication1.MyService.WebService();
11             service.GetNewsAsync(10);
12             service.GetNewsCompleted += new WindowsApplication1.MyService.GetNewsCompletedEventHandler(service_GetNewsCompleted);
13             ChangeProcess();
14         }
15 
16         void service_GetNewsCompleted(object sender, WindowsApplication1.MyService.GetNewsCompletedEventArgs e)
17         {
18             if (e.Error != null)
19             {
20                 throw e.Error;
21             }
22             progressBar1.Value = progressBar1.Maximum;
23             DataSet ds = e.Result;
24             if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
25             {
26                 return;
27             }
28             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
29             {
30                 this.listBox1.Items.Add("title:"+ds.Tables[0].Rows[i]["title"].ToString());
31                 this.listBox1.Items.Add("summary:" + ds.Tables[0].Rows[i]["summary"].ToString());
32             }
33         }
34         void ChangeProcess()
35         {
36             for (int i = 0; i < 10; i++)
37             {
38                 this.progressBar1.Value = i;
39                 //System.Threading.Thread.Sleep(5000);
40             }
41         }
42     }
原文地址:https://www.cnblogs.com/hfliyi/p/2687894.html