Tuesday, April 10, 2012

ASP.NET - jQuery ajax calls queue instead of running concurrently?

I have to do some processing on a file loaded by the user. This process can take a long time based on the number of pages within the file. I am planning on using jQuery UIs progressbar and telling the user how many pages have been processed. However, my progress check does not return until the first ajax call is complete. Both calls complete properly are connecting to the corresponding web methods.



I researched this a little bit already, and I found this answer to another question which pretty much stated that if both calls use the Session they wont process concurrently. I only use the Session in one of the calls though.



What am I missing?



This is the initial call that starts the processing of the file. I set the UpdateProgressBar to run a second after the processing of the file starts.



setTimeout("UpdateProgressBar();", 1000);

$.ajax({
type: "POST",
async: true,
data: // my params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// Do stuff when file is finished processing
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});


This is the UpdateProgressBar function:



function UpdateProgressBar() {
$.ajax({
type: "POST",
async: true,
data: // my params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var totalPages = parseInt($('[id$=lbl_PageCount]').text());
var pagesProcessed = result.d;
var percentProcessed = pagesProcessed / totalPages;

$('#div_ProgressBar').progressbar("value", percentProcessed);

if (pagesProcessed < totalPages)
setTimeout("UpdateProgressBar();", 800);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});


Edit:



This is the WebMethod called by UpdateProgressBar. The other WebMethod does use Session, but as you can see this one does not. They both access a dll, however, I tried testing without using the dll for the update and it did not make a difference.



[WebMethod]
public static int CheckPDFProcessingProgress(// params go here)
{
int pagesProcessed = 1;

try
{
OrderService _orderService = new OrderService();
pagesProcessed = _orderService.GetMailingPDFPagesProcessedProgress(PersonID);
}
catch (Exception ex)
{
// log error
}

return pagesProcessed;
}




1 comment:

  1. I don't think your approach will work because you call setTimeout("UpdateProgressBar();", 800); as part of your success event. The success event is only triggered when the ajax request has returned. This means you are only checking for progress when the processing has already completed.

    ReplyDelete