62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
this.lable1.Text=DateTime.Now.ToString();
SlowTask slowTask1 = new SlowTask();
PageAsyncTask asyncTask1 = new PageAsyncTask(slowTask1.OnBegin, slowTask1.OnEnd, slowTask1.OnTimeout, null);
Page.RegisterAsyncTask(asyncTask1);
Page.ExecuteRegisteredAsyncTasks();
this.lable2.Text = slowTask1.GetAsyncTaskProgress();
}
}
public class SlowTask
{
private String _taskprogress;
private AsyncTaskDelegate _dlgt;
protected delegate void AsyncTaskDelegate();
public String GetAsyncTaskProgress()
{
return _taskprogress;
}
public void ExecuteAsyncTask()
{
Thread.Sleep(TimeSpan.FromSeconds(5.0));
}
public IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData)
{
_taskprogress = "AsyncTask started at: " + DateTime.Now + ". ";
_dlgt = new AsyncTaskDelegate(ExecuteAsyncTask);
IAsyncResult result = _dlgt.BeginInvoke(cb, extraData);
return result;
}
public void OnEnd(IAsyncResult ar)
{
_taskprogress += "AsyncTask completed at: " + DateTime.Now;
_dlgt.EndInvoke(ar);
}
public void OnTimeout(IAsyncResult ar)
{
_taskprogress += "AsyncTask failed to complete " +
"because it exceeded the AsyncTimeout parameter.";
}
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Threading;
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
public delegate void IAsyncDelegate();
DateTime time1;
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
time1 = DateTime.Now;
}
void Page_Load(object sender, EventArgs e)
{
Response.Write("Page_Load: thread #" + System.Threading.Thread.CurrentThread.GetHashCode() + "<br />");
Response.Write("==============<br />");
//分别注释掉以下同步与异步的代码,打印执行时间。
//1 异步
BeginEventHandler bh = new BeginEventHandler(this.BeginGetAsyncData);
EndEventHandler eh = new EndEventHandler(this.EndGetAsyncData);
AddOnPreRenderCompleteAsync(bh, eh);
//2 同步
//show_a();
}
private void show_a()
{
Int64 j = 0;
for (Int64 i = 1; i < 500000000; i++)
{
j = j + i;
}
Response.Write(j.ToString() + "<br />");
Response.Write("Page_Load: thread #" + System.Threading.Thread.CurrentThread.GetHashCode() + "<br />");
}
private void show()
{
Response.Write("BeginGetAsyncData: thread #" + Thread.CurrentThread.GetHashCode() + "<br />");
}
IAsyncResult BeginGetAsyncData(Object src, EventArgs args, AsyncCallback cb, Object state)
{
Int64 j = 0;
for (Int64 i = 1; i < 500000000; i++)
{
j = j + i;
}
Response.Write(j.ToString() + "<br />");
IAsyncDelegate iasy = new IAsyncDelegate(show);
IAsyncResult result = iasy.BeginInvoke(cb, state);
return result;
}
void EndGetAsyncData(IAsyncResult ar)
{
Response.Write("EndGetAsyncData: thread #" + Thread.CurrentThread.GetHashCode() + "<br />");
Response.Write("==============<br />");
}
protected override void OnPreRenderComplete(EventArgs e)
{
base.OnPreRenderComplete(e);
DateTime time2 = DateTime.Now;
TimeSpan ts = time2.Subtract(time1);
Response.Write("加载时间:" + ts.TotalMilliseconds);
}
}
}