Since asp2.0 provides support for asynchronous pages, the performance of asynchronous calls to WebService has been truly improved.
To use an asynchronous page, you must first set Async="true". The asynchronous page is implemented by adding the Begin and end asynchronous methods between the Prerender and PrerenderComplete events. The Begin and End methods belong to different threads.
There are two ways to implement WS asynchronous pages:
1. Use the wait method to implement an asynchronous general class and encapsulate WS
/**//// <summary>
/// Use the wait method to implement asynchronous
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private Account account;
private string username;
public Account Account
{
get { return account; }
set { account = value; }
}
public string Username
{
get { return username; }
set { username = value; }
}
public IAsyncResult BeginAsyncGetAccount(object sender, EventArgs e, AsyncCallback cb, object state)
{
return vb.BeginGetAccountbyName(username,cb,state);
}
public void EndAsyncGetAccount(IAsyncResult ar)
{
account = vb.EndGetAccountbyName(ar);
}
/**//// <summary>
/// Use event-driven asynchronous
/// </summary>
/// <param name="username"></param>
public void GetAccountCompleted(Object source, VB.GetAccountbyNameCompletedEventArgs e)
{
account = e.Result;
}
public void AsGetAccount(string username)
{
vb.GetAccountbyNameCompleted += new GetAccountbyNameCompletedEventHandler(GetAccountCompleted);
vb.GetAccountbyNameAsync(username)
}Call method
protected void Page_Load(object sender, EventArgs e)
{
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
b.Username = "dinghao";
AddOnPreRenderCompleteAsync(b.BeginAsyncGetAccount, b.EndAsyncGetAccount);
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
//End of asynchronous call
VB.Account a = b.Account;
AccountIf ai = new AccountIf(a);
ais[0] = ai;
GridView1.DataSource = ais;
GridView1.DataBind();
}Since the two delegates of AddOnPreRenderCompleteAsync are both of Void type, attributes with return values such as Account should be added to the general class for use by the main calling method. In addition, there is no parameter information for asynchronous methods in the delegate. Parameter attributes should be added such as: Username
2. Event-driven asynchronous (new in 2.0)
Calling method:
protected void Page_Load(object sender, EventArgs e)
{
this.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
b.AsGetAccount("dinghao");
}
protected void Page_PreRenderComplete(object sender, EventArgs e)
{
//End of asynchronous call
VB.Account a = b.Account;
AccountIf ai = new AccountIf(a);
ais[0] = ai;
GridView1.DataSource = ais;
GridView1.DataBind();
}This calling method uses the *Completed event, which is triggered when *Async is completed. This calling method can omit the Account and Username attributes, and is relatively simple to use
http://bluewater.cnblogs.com/archive/2006/ 06/20/430758.html