示例1.演示异步获取一个网址的内容,处理后显示在OutPut这一Label上
1
using System;
2
using System.Web;
3
using System.Web.UI;
4
using System.Web.UI.WebControls;
5
using System.Net;
6
using System.IO;
7
using System.Text;
8
using System.Text.RegularExpressions;
9![](/Images/OutliningIndicators/None.gif)
10
public partial class AsyncPage : System.Web.UI.Page
11![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
12
private WebRequest _request;
13![](/Images/OutliningIndicators/InBlock.gif)
14
void Page_Load (object sender, EventArgs e)
15![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
AddOnPreRenderCompleteAsync (
17
new BeginEventHandler(BeginAsyncOperation),
18
new EndEventHandler (EndAsyncOperation)
19
);
20
}
21![](/Images/OutliningIndicators/InBlock.gif)
22
IAsyncResult BeginAsyncOperation (object sender, EventArgs e,
23
AsyncCallback cb, object state)
24![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
_request = WebRequest.Create("http://msdn.microsoft.com");
26
return _request.BeginGetResponse (cb, state);
27
}
28
void EndAsyncOperation (IAsyncResult ar)
29![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
30
string text;
31
using (WebResponse response = _request.EndGetResponse(ar))
32![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
using (StreamReader reader =
34
new StreamReader(response.GetResponseStream()))
35![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
text = reader.ReadToEnd();
37
}
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40
Regex regex = new Regex ("href\\s*=\\s*\"([^\"]*)\"",
41
RegexOptions.IgnoreCase);
42
MatchCollection matches = regex.Matches(text);
43![](/Images/OutliningIndicators/InBlock.gif)
44
StringBuilder builder = new StringBuilder(1024);
45
foreach (Match match in matches)
46![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
47
builder.Append (match.Groups[1]);
48
builder.Append("<br/>");
49
}
50![](/Images/OutliningIndicators/InBlock.gif)
51
Output.Text = builder.ToString ();
52
}
53
}
54![](/Images/OutliningIndicators/None.gif)
55![](/Images/OutliningIndicators/None.gif)
示例2:演示如何异步从数据库查询数据,并将返回的DataReader绑定到指定控件上
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
![](/Images/OutliningIndicators/None.gif)
public partial class AsyncDataBind : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private SqlConnection _connection;
private SqlCommand _command;
private SqlDataReader _reader;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Hook PreRenderComplete event for data binding
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
![](/Images/OutliningIndicators/InBlock.gif)
// Register async methods
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
}
}
IAsyncResult BeginAsyncOperation (object sender, EventArgs e,
AsyncCallback cb, object state)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string connect = WebConfigurationManager.ConnectionStrings
["PubsConnectionString"].ConnectionString;
_connection = new SqlConnection(connect);
_connection.Open();
_command = new SqlCommand(
"SELECT title_id, title, price FROM titles", _connection);
return _command.BeginExecuteReader (cb, state);
}
![](/Images/OutliningIndicators/InBlock.gif)
void EndAsyncOperation(IAsyncResult ar)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_reader = _command.EndExecuteReader(ar);
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_PreRenderComplete(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Output.DataSource = _reader;
Output.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_connection != null) _connection.Close();
base.Dispose();
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
示例3:异步调用WebService
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
![](/Images/OutliningIndicators/None.gif)
public partial class AsyncWSInvoke1 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private WS.PubsWebService _ws;
private DataSet _ds;
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Hook PreRenderComplete event for data binding
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
![](/Images/OutliningIndicators/InBlock.gif)
// Register async methods
AddOnPreRenderCompleteAsync(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation)
);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
IAsyncResult BeginAsyncOperation (object sender, EventArgs e,
AsyncCallback cb, object state)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_ws = new WS.PubsWebService();
// Fix up URL for call to local VWD-hosted Web service
_ws.Url = new Uri(Request.Url, "Pubs.asmx").ToString();
_ws.UseDefaultCredentials = true;
return _ws.BeginGetTitles (cb, state);
}
![](/Images/OutliningIndicators/InBlock.gif)
void EndAsyncOperation(IAsyncResult ar)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_ds = _ws.EndGetTitles(ar);
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_PreRenderComplete(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Output.DataSource = _ds;
Output.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_ws != null) _ws.Dispose();
base.Dispose();
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
示例4:还是异步调用WebService,但换了一种方法
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
![](/Images/OutliningIndicators/None.gif)
public partial class AsyncWSInvoke2 : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private WS.PubsWebService _ws;
private DataSet _ds;
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (!IsPostBack)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Hook PreRenderComplete event for data binding
this.PreRenderComplete +=
new EventHandler(Page_PreRenderComplete);
![](/Images/OutliningIndicators/InBlock.gif)
// Call the Web service asynchronously
_ws = new WS.PubsWebService();
_ws.GetTitlesCompleted += new
WS.GetTitlesCompletedEventHandler(GetTitlesCompleted);
_ws.Url = new Uri(Request.Url, "Pubs.asmx").ToString();
_ws.UseDefaultCredentials = true;
_ws.GetTitlesAsync();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
void GetTitlesCompleted(Object source,
WS.GetTitlesCompletedEventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_ds = e.Result;
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_PreRenderComplete(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Output.DataSource = _ds;
Output.DataBind();
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (_ws != null) _ws.Dispose();
base.Dispose();
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
示例5:跟示例1差不多,但是加了TimeOut处理
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
![](/Images/OutliningIndicators/None.gif)
public partial class AsyncPageTask : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
private WebRequest _request;
![](/Images/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
PageAsyncTask task = new PageAsyncTask(
new BeginEventHandler(BeginAsyncOperation),
new EndEventHandler(EndAsyncOperation),
new EndEventHandler(TimeoutAsyncOperation),
null
);
RegisterAsyncTask(task);
}
![](/Images/OutliningIndicators/InBlock.gif)
IAsyncResult BeginAsyncOperation(object sender, EventArgs e,
AsyncCallback cb, object state)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_request = WebRequest.Create("http://msdn.microsoft.com");
return _request.BeginGetResponse(cb, state);
}
![](/Images/OutliningIndicators/InBlock.gif)
void EndAsyncOperation(IAsyncResult ar)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string text;
using (WebResponse response = _request.EndGetResponse(ar))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
using (StreamReader reader =
new StreamReader(response.GetResponseStream()))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
text = reader.ReadToEnd();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
Regex regex = new Regex("href\\s*=\\s*\"([^\"]*)\"",
RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(text);
![](/Images/OutliningIndicators/InBlock.gif)
StringBuilder builder = new StringBuilder(1024);
foreach (Match match in matches)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
builder.Append(match.Groups[1]);
builder.Append("<br/>");
}
![](/Images/OutliningIndicators/InBlock.gif)
Output.Text = builder.ToString();
}
![](/Images/OutliningIndicators/InBlock.gif)
void TimeoutAsyncOperation(IAsyncResult ar)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Output.Text = "Data temporarily unavailable";
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)