ASP.NETには、要求を開始できる非同期ハンドラーが組み込まれています。これは、非ハンドラースレッドで実行され、すべて組み込みの3番目のスレッド(元の要求ハンドラースレッドとは異なります)で完了する場合があります。私はそれを何度も使用しました。
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();
}