ASP.NET 具有内置的异步处理程序,可让您启动请求,它在非处理程序线程上运行,并且可以在所有内置的第三个线程(不同于原始请求处理程序线程)上完成。我已经用过很多次了。
http://msdn.microsoft.com/en-us/magazine/cc163725.aspx
给自己写一个小助手方法来连接它。
/// <summary>
/// On load event override
/// </summary>
/// <param name="e">arguments to the event</param>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string query = this.Page.Request.QueryString["query"];
if (!string.IsNullOrEmpty(query))
{
var pat = new PageAsyncTask(this.BeginAsync, this.EndAsync, this.TimeOut, query);
this.Page.RegisterAsyncTask(pat);
}
string me = string.Format(Format, System.Threading.Thread.CurrentThread.ManagedThreadId, "Onload");
Trace.Write(me);
}
protected override void Render(HtmlTextWriter writer)
{
string me = string.Format(Format, System.Threading.Thread.CurrentThread.ManagedThreadId, "Render");
Trace.Write(me);
this.Icompleted.Text = DateTime.Now.ToString();
base.Render(writer);
}
/// <summary>
/// start the async task
/// </summary>
/// <param name="sender">original caller</param>
/// <param name="e">unused arguments</param>
/// <param name="cb">call back routine</param>
/// <param name="state">saved stated</param>
/// <returns>IAsyncResult to signal ender</returns>
private IAsyncResult BeginAsync(object sender, EventArgs e, AsyncCallback cb, object state)
{
this.bsc = new YourWebServiceReferenceGoesHere();
return this.bsc.BeginGetResponseXml("1", (string)state, "10", "1", cb, state);
}
/// <summary>
/// when the task completes
/// </summary>
/// <param name="ar">the async result</param>
private void EndAsync(IAsyncResult ar)
{
XmlResponse response = this.bsc.EndGetResponseXml(ar);
this.bsc.Close();
this.bsc = null;
this.PostProcess(response);
}
private void TimeOut(IAsyncResult ar)
{
// currently we do nothing here.
}
/// <summary>
///
/// </summary>
private void PostProcess(XmlResponse response )
{
string me = string.Format(Format, System.Threading.Thread.CurrentThread.ManagedThreadId, "bingsearch");
Trace.Write(me);
var xds = new XmlDataSource
{
EnableCaching = false,
CacheDuration = 0,
Data = response.Xml,
Transform = this.RemoveNamespaces()
};
this.some.DataSource = xds;
this.some.DataBind();
}