我正在学习 ASP .NET 中的异步操作。我在这里找到了一篇文章MSDN。该代码有效,但我希望能够在漫长的过程中更新 UI。它仅在过程完成后显示结果。我对 AsyncClass 使用与提到的链接中相同的代码。这是代码:
AsyncTask slowTask1 = new AsyncTask();
PageAsyncTask task = new PageAsyncTask(slowTask1.OnBegin, slowTask1.OnEnd, slowTask1.OnTimeout, "Async1", true);
Page.AsyncTimeout = new TimeSpan(0, 0, 5);
Page.RegisterAsyncTask(task);
// Page.ExecuteRegisteredAsyncTasks();
IAsyncResult result = slowTask1.OnBegin(this.Page,null,null,null);
WaitHandle waitHandle = result.AsyncWaitHandle;
waitHandle.WaitOne(2000, false);
if(result.IsCompleted)
{
slowTask1.OnEnd(result);
lbProgress.Text = slowTask1.GetAsyncTaskProgress();
Thread.Sleep(1000);
}
else
lbProgress.Text = slowTask1.GetAsyncTaskProgress();