0

I have a PageAsyncTask that takes about 2 mins to run. My page directive has Async="true", my config has the timeout as 180 (3 mins to give extra time), but when I call Page.ExecuteRegisteredAsyncTasks() it doesn't come back right away and is blocking while it's running the task.

Am I missing any steps to get this to run asynchronously?

(this was formatted better but after edit it screwed it all up)

// in my button click event
MyTask task = new MyTask();
PageAsyncTask asyncTask = new PageAsyncTask(task.OnBegin, task.OnEnd, task.OnTimeout);
Page.RegisterAsyncTask(asyncTask);
Page.ExecuteRegisteredAsyncTasks();


// MyTask.cs
public class MyTask
{
    protected delegate void AsyncTaskDelegate();
    private AsyncTaskDelegate dlgt;

    public void Run() { // my code that hits DB }

    public IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extra)
    {
        dlgt = new AsyncTaskDelegate();
        IAsyncResult result = dlgt.BeginInvoke(extra, cb, null);
        return result;
    }
}
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
user441521
  • 6,942
  • 23
  • 88
  • 160
  • Keep in mind that just putting `async` on a method doesn't actually make it asynchronous. If you just do a bunch of synchronous work in a method marked as `async` it's still a synchronous method. – Servy Oct 15 '13 at 19:59
  • I guess what I'm expecting out of this is that the page doesn't sit there with the loading bar running until the process is done. Once I click the button to run this task I'm expecting the page to come right back while it runs in the background. So yes, I want my synchronous method to run in the background while my page comes back right away. Is that not correct? – user441521 Oct 15 '13 at 20:03
  • @user441521 That is not correct. Asynchronous page support simply means that the page isn't consuming a thread pool thread while it performs long running non-CPU bound work, and that that the thread pool thread can be spending it's time working on another response at that time. It has no real change on the user experience, beyond performance. – Servy Oct 15 '13 at 20:06
  • So I've been the only one testing my site so far, so are you saying without Asynchronous page support that while it's running my long running code other people couldn't hit my page until it's done? So adding asynchronous page support will let other people access the page while it's running a number of other long running requests? – user441521 Oct 15 '13 at 20:09
  • So how can I fire off asynchronous tasks that will have the page come right back to the user so they don't have to wait for 2 mins while it's running? – user441521 Oct 15 '13 at 20:11
  • @user441521 If you don't need to update the UI with the result then just create a new thread, or pass off a new item to the thread pool, and don't do anything else with it. When you register the async operation you're specifically telling the page to wait for that to finish before it response. Just don't do that if you don't want it to wait. Obviously you then can't change the response once it's been sent. – Servy Oct 15 '13 at 20:13
  • I would recommend looking into pagemethods/webmethods. These are server side methods you can call via javascript, that don't trigger a postback. That way, the user can click a button, it will trigger a method to run on the server in the background and the user doesn't have to wait. Of course, all this depends on the data that you need to supply to the method and what kind of response you are looking to receive. – Smeegs Oct 15 '13 at 20:22
  • I'm uploading csv files and some text. The tasks results in data getting written to the DB and I have a gridview on the page that uses ajax to refresh the gridview so they can see the results of the operation that way. Thanks for the suggestions. – user441521 Oct 15 '13 at 20:27

1 Answers1

2

I misunderstood what this was about. I just went with making a new thread for the task as the main point of the task is to insert records into the DB and my page has a gridview on it reading the DB using ajax which shows the progress. So as the thread is working you can see record counts go up.

Thread myThread = new Thread(new ParameterizedThreadStart(task.Run));
myThread.Start(extra);

Thanks!

user441521
  • 6,942
  • 23
  • 88
  • 160
  • For my gridview I assume you mean? No, I just have a refresh button they can click. Side question, are threads like this linked to the page being open? When testing as soon as I closed my browser and it went back to VS the thread seems to have stopped. With IIS express still running I would think the thread would still continue. I know before I did the thread way even if I closed the page it was still adding records to the DB in the background. – user441521 Oct 15 '13 at 20:52