Click to See Complete Forum and Search --> : Making AJAX calls to server side to check long running task progress


trivektor
12-22-2008, 11:24 PM
How can I make AJAX calls to server side to keep user posted of the progress of a long running task? I've tried to make some calls but they only executed after the task was completed. Any suggestion? Thanks.

chazzy
12-23-2008, 08:26 AM
how are you running this long running task? is it in a separate thread?

trivektor
12-23-2008, 10:17 AM
Yes, it runs on a seperate thread. Let me give a bit more information. When I click submit in my application, it will start a thread that execute the long running task, then redirects me to a new page, say executing.aspx. On this page, I want to be able to make Ajax calls to another page called status.aspx which will return me the variable Application["currentJob"], that I can use to output on executing.aspx to inform the user of the progress, for instance

Executing job 1, then
Executing job 2, then
Executing job 3 .... (this is done by JS)

The problem that I have is, when I make Ajax calls to status.aspx to access the Application["currentJob"] variable, the calls will not be made until the long running task is completed.

chazzy
12-23-2008, 04:15 PM
how does status check what's going on?

trivektor
12-23-2008, 09:41 PM
Like I said above, make an Ajax call to status.aspx, have it returned Application["currentJob"] variable in its response. Then use JS to output that content to the user.

chazzy
12-23-2008, 09:54 PM
no, that's how your app communicates w/ status. i want to know how status communicates with this long running process (post relevant code)

trivektor
12-23-2008, 11:09 PM
I stored the current job number in Application["currentJob"] and update it in a loop

for (int i = 1; i < jobQueue.Length; i++) {
doJob();
Application["currentJob"] = i;
}

chazzy
12-23-2008, 11:31 PM
I take it that "doJob" is the long running process? you need to run the method from a separate thread. if you're creating the thread in "doJob" then it won't return until the thread has completed.

what i was assuming was that you create the thread in the background at som previous page and are looking at the status continually.

trivektor
12-23-2008, 11:43 PM
Yes, doJob is the long running process. I created the thread in a previous page, and was redirected to a new page. It is on this new page that I want to see the progress updated continuously and asynchronously.

trivektor
12-23-2008, 11:48 PM
So do you have any suggestion for me on where to create the thread and where to update the progress?

chazzy
12-24-2008, 12:52 AM
well like you mentioned, you're starting it on another page. no reason to start it again on here. you need to just keep a reference to the Thread somewhere - session? maybe?

trivektor
12-24-2008, 01:08 AM
I think I have misled you. Let me word it again. When I click submit, I start a thread to doJob and is redirected to a new page where I can see a spinning circle and the progress of doJob. That is, I will see "Doing job 1, doing job 2, doing job 3 etc". I'm just stuck on how to access to Application["currentJob"] variable and use javascript to write it to the screen.