异步WEB SERVICES

异步WEB SERVICES

如果想让你的客户端应用程序在等待WEB SERVICES的响应是继续处理其他的任务或让应用程序允许用户交互,那么可以使用WEB SERVICES的异步方法.不过,应该避免使用,因为异步通信让你的应用程序变得复杂并且难以调试,此外,异步请求不一定按照他们提交的顺序返回,使用不当的话,异步请求是你的程序出现异常行为,让用户无所适从.

下面是代码片段:
[WebMethod]
public string HelloWorld(string name)
{//让线程休眠1到7秒,降低测试的速度
Thread.Sleep((new Random()).Next(1000, 7000));
return "Hello World"+name;
}
客户端代码:
partial class Form1 : Form
{
async.Service s=new testasync.async.Service();
AsyncCallback callback;

public Form1()
{
InitializeComponent();
callback = new AsyncCallback(hello);//回调委托
}

private void button1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
{//第一个参数是方法的参数,跟着是回调委托,用于回调是调用方法,最后的是获取任何对象的引用,他可以将某个请求与相关信息管理起来,这里使用一个LABEL控件的引用来返回结果
s.BeginHelloWorld(this.textBox1.Text, callback, this.label1);
}
if (this.textBox2.Text != "")
{
s.BeginHelloWorld(this.textBox2.Text, callback, this.label2);
}
if (this.textBox3.Text != "")
{
s.BeginHelloWorld(this.textBox3.Text, callback, this.label3);
}
}
private void hello(IAsyncResult result)
{//IAsyncResult可以访问等待处理的异步操作状态的有用信息
/IAsyncResult还有一个ASYNCWAITHANDLE属性,他有一个WAITHANDLE对象,你可以用他来挂起应用程序的执行,直到相关的异步操作完成,WAITHANDLE有一个静态方法,允许你对多个异步操作进行同步.
WAITHANDLE对象的三个方法,1)WAITONE等待单个的WAITHANDLE对象 2)WAITALL静态的方法,它等待WAITHANDLE[]中包含的所有WAITHANDLE对象完成 3)WAITANY静态的方法,它等待WAITHANDLE[]中包含的任何一个WAITHANDLE对象完成 这里没有使用到,但某些场合会很有用
/
Label result1 = (Label)result.AsyncState;
string r = s.EndHelloWorld(result);//返回结果
Updater updater = new Updater(r, result1);
result1.Invoke(new MethodInvoker(updater.Update));

}
}
有一个问题就是,回调方法不会与调用代码在相同的线程执行.这意味着它可能会在执行应用程序另一部分的同时又执行.所以可以使用INVOKE()方法,将控制方法调用操作封送(MARSHAL)到用户线程.这里用了一个UPDATER.
public class Updater
{
private string latext;
private Control control;
public Updater(string text, Control c)
{
this.latext = text;
this.control = c;
}
public void Update()
{
this.control.Text = latext;
}
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus