asp.net 3.5异步调用方法小结
web请求
RssAsync.aspx
//这是调用本网站的两个页面,也可以设置成外部网站页面
const string rssFeed1 = "http://localhost:2289/AsnicTest/WebRequest1.aspx";
const string rssFeed2 = "http://localhost:2289/AsnicTest/WebRequest2.aspx";
AsyncReader rss1, rss2;
protected void Page_Load(object sender, EventArgs e)
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
rss1 = new AsyncReader(rssFeed1);
rss2 = new AsyncReader(rssFeed2);
PageAsyncTask task1 = new PageAsyncTask(new BeginEventHandler(rss1.BeginRead), new EndEventHandler(rss1.EndRead), null, null, true);
PageAsyncTask task2 = new PageAsyncTask(new BeginEventHandler(rss2.BeginRead), new EndEventHandler(rss2.EndRead), null, null, true);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = rss1.GetData();
Literal1.Text += rss2.GetData();
}
AsyncReader是封装的一个类,代码如下:const string rssFeed1 = "http://localhost:2289/AsnicTest/WebRequest1.aspx";
const string rssFeed2 = "http://localhost:2289/AsnicTest/WebRequest2.aspx";
AsyncReader rss1, rss2;
protected void Page_Load(object sender, EventArgs e)
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
rss1 = new AsyncReader(rssFeed1);
rss2 = new AsyncReader(rssFeed2);
PageAsyncTask task1 = new PageAsyncTask(new BeginEventHandler(rss1.BeginRead), new EndEventHandler(rss1.EndRead), null, null, true);
PageAsyncTask task2 = new PageAsyncTask(new BeginEventHandler(rss2.BeginRead), new EndEventHandler(rss2.EndRead), null, null, true);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = rss1.GetData();
Literal1.Text += rss2.GetData();
}
Code
WebService异步调用
1.asp.net 2.0开始,提供简单的方法实现异步调用
AsyncWebService.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
this.getHello();
}
private void getHello()
{
webServerAsync.WebService1 hello = new webServerAsync.WebService1();
///同步调用WebService方法
//Literal1.Text = hello.HelloAsync("张三", 30);
//异步调用WebService方法
hello.HelloWorldCompleted += new webServerAsync.HelloWorldCompletedEventHandler(hello_HelloWorldCompleted);
hello.HelloWorldAsync("张三", 20);
}
void hello_HelloWorldCompleted(object sender, webServerAsync.HelloWorldCompletedEventArgs e)
{
Literal1.Text = e.Result;
}
{
this.getHello();
}
private void getHello()
{
webServerAsync.WebService1 hello = new webServerAsync.WebService1();
///同步调用WebService方法
//Literal1.Text = hello.HelloAsync("张三", 30);
//异步调用WebService方法
hello.HelloWorldCompleted += new webServerAsync.HelloWorldCompletedEventHandler(hello_HelloWorldCompleted);
hello.HelloWorldAsync("张三", 20);
}
void hello_HelloWorldCompleted(object sender, webServerAsync.HelloWorldCompletedEventArgs e)
{
Literal1.Text = e.Result;
}
为了调用方便,以及代码重用,我也封装了一个类
AsyncWebService.aspx.cs
helloRader hello1, hello2;
public string helloData;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
dataBind();
}
private void dataBind()
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
hello1 = new helloRader("张三", 20);
hello2 = new helloRader("李四", 22);
PageAsyncTask task1 = AsyncTask(hello1);
PageAsyncTask task2 = AsyncTask(hello2);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
private PageAsyncTask AsyncTask(helloRader hello)
{
return new PageAsyncTask(new BeginEventHandler(hello.BeginRead), new EndEventHandler(hello.EndRead), null, null, true);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = hello1.GetData() + "</br>";
Literal1.Text += hello2.GetData();
}
public string helloData;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
dataBind();
}
private void dataBind()
{
PreRenderComplete += new EventHandler(RssAsync_PreRenderComplete);
hello1 = new helloRader("张三", 20);
hello2 = new helloRader("李四", 22);
PageAsyncTask task1 = AsyncTask(hello1);
PageAsyncTask task2 = AsyncTask(hello2);
RegisterAsyncTask(task1);
RegisterAsyncTask(task2);
}
private PageAsyncTask AsyncTask(helloRader hello)
{
return new PageAsyncTask(new BeginEventHandler(hello.BeginRead), new EndEventHandler(hello.EndRead), null, null, true);
}
void RssAsync_PreRenderComplete(object sender, EventArgs e)
{
Literal1.Text = hello1.GetData() + "</br>";
Literal1.Text += hello2.GetData();
}
helloRader.cs
Code
源码下载