zoukankan      html  css  js  c++  java
  • 异步调用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     }
  • 相关阅读:
    Orleans 2 实例
    Linux基础1 目录和文件系统
    C#中的异步多线程补充1
    委托的小例子(基本委托,匿名方法,lambda)
    Orleans 1 基本概念
    WPF10 Binding-2
    WPF9 Binding-1
    WPF8 UI布局
    WPF7 布局控件
    软工总结
  • 原文地址:https://www.cnblogs.com/hfliyi/p/2687894.html
Copyright © 2011-2022 走看看