日期:2014-05-17  浏览次数:21078 次

请教一下异步调用WS的问题
本帖最后由 ilearn 于 2012-12-04 10:51:44 编辑 网上的例子(包括MSDN)都是说 BeginXXXX( BeginOperationName) 和 endXXXX(endOperationName)事件的,可是我怎么也找不到这些事件,为什么?能给个例子吗?谢谢,我是C# 2005

http://msdn.microsoft.com/zh-cn/library/ms228968%28v=vs.80%29.aspx
------最佳解决方案--------------------
几种方式你参数一下:
1.protected void Button1_Click(object sender, EventArgs e)
   {
       AsyncCallback callback = new AsyncCallback(GetDataCallBack);
       localhost.TheFirstWebservice webservice = new localhost.TheFirstWebservice();
       IAsyncResult result = webservice.BeginGetData(callback, webservice);
?
1
2
3
4
5
6
7
8
9
10
11
    //当异步调用没有结束时
    while (result.IsCompleted)
    {
        //客户端代码
     }
}
public void GetDataCallBack(IAsyncResult result)
{
    localhost.TheFirstWebservice webservice = (localhost.TheFirstWebservice)result.AsyncState;
    DataSet ds = webservice.EndGetData(result);
}
2.等待模式
protected void Button1_Click(object sender, EventArgs e)
   {
       localhost.TheFirstWebservice webservice = new localhost.TheFirstWebservice();
       IAsyncResult result = webservice.BeginGetData(null, null);
       result.AsyncWaitHandle.WaitOne();
       DataSet ds = webservice.EndGetData(result);
   }
3.事件驱动模式
private void BtnGoAsync_Click(object sender, EventArgs e)
       {
           //创建代理类对象
           localhost.TheFirstWebservice objService = new WebServiceForm.localhost.TheFirstWebservice();
           objService.HelloWorldCompleted += new localhost.HelloWorldCompletedEventHandler(HelloWorldCompletedFunc);
           objService.HelloWorldAsync("px");
           TxtAsync1.Text = DateTime.Now.ToString("mm:ss.ffff");
       }
       public void HelloWorldCompletedFunc(object sender, localhost.HelloWorldCompletedEventArgs e)
       {
           TxtAsync2.Text = e.Result;
       }
------其他解决方案--------------------

http://www.cnblogs.com/menglin2010/archive/2012/03/30/2423679.html
------其他解决方案--------------------
我在网上找到你的例子
http://www.cnblogs.com/smallstone/archive/2010/05/27/1745275.html
我就是找不到 IAsyncResult result = webservice.BeginGetData(null, null);
这个 beginxxxx的方法
------其他解决方案--------------------
我在异步执行过程中不需要等待的,我在异步执行过程中执行其它东西,最后异步执行以后返回一个结果就可以了