1

I have a function which have a long execution time.

 public void updateCampaign()
    {

        context.Session[processId] = "0|Fetching Lead360 Campaign";
        Lead360 objLead360 = new Lead360();
        string campaignXML = objLead360.getCampaigns();

        string todayDate = DateTime.Now.ToString("dd-MMMM-yyyy");
        context.Session[processId] = "1|Creating File  for Lead360 Campaign on " + todayDate;
        string fileName = HttpContext.Current.Server.MapPath("campaigns") + todayDate + ".xml";
        objLead360.createFile(fileName, campaignXML);

        context.Session[processId] = "2|Reading The latest Lead360 Campaign";
        string file = File.ReadAllText(fileName);

        context.Session[processId] = "3|Updating Lead360 Campaign";
        string updateStatus = objLead360.updateCampaign(fileName);
        string[] statusArr = updateStatus.Split('|');
        context.Session[processId] = "99|" + statusArr[0] + " New Inserted , " + statusArr[1] + " Updated , With " + statusArr[2] + " Error , ";
    }

So to track the Progress of the function I wrote a another function

public void getProgress()
    {
        if (context.Session[processId] == null)
        {
            string json = "{\"error\":true}";
            Response.Write(json);
            Response.End();
        }else{
            string[] status = context.Session[processId].ToString().Split('|');
            if (status[0] == "99") context.Session.Remove(processId);

            string json = "{\"error\":false,\"statuscode\":" + status[0] + ",\"statusmsz\":\"" + status[1] + "\" }";
            Response.Write(json);
            Response.End();  
        }
    }

To call this by jQuery post request is used

reqUrl = "AjaxPages/lead360Campaign.aspx?processid=" + progressID + "&action=updatecampaign";
$.post(reqUrl);
setTimeout(getProgress, 500);

get getProgress is :

function getProgress() {
    reqUrl = "AjaxPages/lead360Campaign.aspx?processid=" + progressID + "&action=getProgress";
    $.post(reqUrl, function (response) {
        var progress = jQuery.parseJSON(response);
        console.log(progress)
        if (progress.error) {
            $("#fetchedCampaign .waitingMsz").html("Some error occured. Please try again later.");
            $("#fetchedCampaign .waitingMsz").css({ "background": "url(common/images/ajax_error.jpg) no-repeat center 6px" });
            return;
        }
        if (progress.statuscode == 99) {
            $("#fetchedCampaign .waitingMsz").html("Update Status :"+ progress.statusmsz );
            $("#fetchedCampaign .waitingMsz").css({ "background": "url(common/images/ajax_loded.jpg) no-repeat center 6px" });
            return;
        }
        $("#fetchedCampaign .waitingMsz").html("Please Wait... " + progress.statusmsz);
        setTimeout(getProgress, 500);
    });
}

But the problem is that I can't see the intermediate message. Only the last message is been displayed after a long lime of ajax loading message

Also on the browser console I just see that after a long time first requested is completed and after that the second request is completed. but there should be for getProgress ?

I have checked jquery.doc and it says that $post is an asynchronous request.

Can anyone please explain what is wrong with the code or logic?

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
GajendraSinghParihar
  • 9,051
  • 11
  • 36
  • 64

2 Answers2

3

You are in a situation discussed here:

ASP.net session request queuing

While a request for a given user's session is processed, other requests for the same session are waiting. You need to run your long function in a background thread and let the request that initiates it finish. However, the background thread will not have access to session, and you will need a different mechanism to communicate its progress.

Community
  • 1
  • 1
Igor
  • 15,833
  • 1
  • 27
  • 32
1

From the information you've provided, I would suspect that it's not your javascript code that's being synchronous, but rather the server-side code. You can test this by using Firebug or Chrome's dev tools to look at the start and end times of the two AJAX requests. If I'm right, you'll see that the second request begins after half a second, but doesn't complete until after the first one.

If that's the case, possible causes are:

  • Running in a dev environment in Visual Studio, especially in debug mode, seems to reduce the amount of asynchronicity. The dev environment seems to like to process one request at a time.
  • See Igor's answer about session request queueing.
  • You may have code that explicitly locks resources and causes the second request to block until the long-running request is done.

One other possible culprit is the fact that most browsers only allow a limited number of concurrent requests to a particular domain. If you have a few requests pending at any given moment, the browser may just be queuing up the remaining requests until they return.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • can <%@Page EnableSessionState="false"/> help me in any manner? if not what will you suggest me to do ? – GajendraSinghParihar Dec 06 '12 at 17:26
  • @Champ: As with many things, the answer is: it depends. Take some time to consider the implications of locking on session state, or disallowing access to session state. Chances are, your best bet would be to allow the initial request to complete, showing that the long-running job has successfully begun, and then have the browser send new requests periodically to check the status. Once the status is Complete, do what you would have otherwise done when the original request completed. – StriplingWarrior Dec 06 '12 at 17:33
  • but by this i cant moniter the progress of the long job :( – GajendraSinghParihar Dec 06 '12 at 17:35