0

I am learning about asynchronous operations in ASP .NET. I found an article here MSDN. The code works, but I want to be able to update the UI during the long process. It only shows the result after the process has finished. I am using the same code for the AsyncClass as in the link mentioned. Here is the code:

            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();
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
coffeeak
  • 2,980
  • 7
  • 44
  • 87

1 Answers1

0

You cannot do it from code behind. While the response is sent to client that request/response is ended. So if you need to wait for the result of Async operation (somehow used in response) and still need to update page, use AJAX and pull data from client side.

Xaqron
  • 29,931
  • 42
  • 140
  • 205