【 tulaoshi.com - ASP.NET 】
关于webservice的异步调用简单实例无论在任何情况下,被调用方的代码无论是被异步调用还是同步调用的情况下,被调用方的代码都是一样的,
下面,我们就以异步调用一个webservice 为例作说明。这是一个webservice
_ Public Function delCurTable(ByVal tbName As String) As Boolean Try Return True Catch ex As Exception Return False End Try End Function
关于这个webservice的同步调用方法,我就不做说明了异步调用: Private a As vbwebservice.DataManage'定义一个webservice变量 Dim ar As IAsyncResultpublic sub Callback1(byval ar as IAsyncResult)''这里可以不写任何代码'这里的代码在调用方运行结束后,开始运行end sub
#region "异步调用"private sub btnStart_Click()dim cb as asyncallback=new asyncallback(address(me.callback1))ar=a.begindelcurtable("zdf",cb,a)end sub
private sub btnEnd_Click()if ar is nothing thenexit subend ifif ar.IsCompleted Thendim m as boolean=a.enddelcurtable(ar)messagebox.show(m.tostring)end ifend sub#end region注意:“返回异步调用的结果一次只能调用一次,如果想再执行一遍,就必须重新执行一遍begin的方法”以上是vb.net的用法,下面是c#的用法CWebService.DataManage a; System.IAsyncResult ar; private void Form1_Load(object sender, System.EventArgs e) { a=new CWebService.DataManage(); }
public void CallBack1(System.IAsyncResult ar) { }
private void btnStart_Click(object sender, System.EventArgs e) { System.AsyncCallback cb=new AsyncCallback(this.CallBack1); ar=a.BegindelCurTable("zhang",cb,a); }
private void btnEnd_Click(object sender, System.EventArgs e) { if(ar==null) { MessageBox.Show("null"); return ; } if(ar.IsCompleted ) { bool m=a.EnddelCurTable(ar); MessageBox.Show(m.ToString()); } else { MessageBox.Show("为完成"); } }