简体   繁体   中英

progress bar using multiple ajax calls for asp.net

I have an application that has a long time processing in code behind. I was thinking on starting the long time processing by calling from javascript a page,

    function OnCopy(type){
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
        //window.clearInterval(interval_handler);
        alert('done');
        }
    }
    xmlhttp.open("GET", "<%=Request.Path %>?copy=" + type, true);
    xmlhttp.send();
    interval_handler = window.setInterval(OnCheckStatus, 1000);   
}

The last line of this function will start a timer, to check every second the status:

    function OnCheckStatus(){
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            //window.clearInterval(interval_handler);
            //var result = eval(xmlhttp.responseText);
            //var pb = document.getElementById('progressbar_' + result[0]);
            //if(pb != null)
            //    pb.innerHTML = result[1] + ' % - ' + result[2];
            debug++;
            var pb = document.getElementById('progressbar_test');
            pb.innerHTML = debug;
        }
    }

    xmlhttp.open("GET", "<%=Request.Path %>?checkstatus=1", true);
    xmlhttp.send();
}

On code behind I have this long time processing function and check status function:

    private void Copy(string type)
    {
        Application["ProgressBar.Type"] = type;

        for (int i = 0; i < 100; i++)
        {
            System.Threading.Thread.Sleep(100);
            Application["ProgressBar.Value"] = i.ToString();
        }

        Application["ProgressBar.Type"] = null;
        Application["ProgressBar.Value"] = null;
    }

    private void CheckStatus()
    {
        Response.Clear();

        string type = (string)Application["ProgressBar.Type"];
        string value = (string)Application["ProgressBar.Value"];

        if (type == null) type = "";
        if (value == null) value = "";

        string response = "['" + type + "','" + value + "','" + DateTime.Now.ToString("HH:mm:ss") + "']";
        Response.Write(response);
        Response.End();
    }

Now, the problem is that until the Copy function is not finished, there is no answer on the CheckStatus function from code behind (I think all calls are queued), but, when is finished, the displayed debug value is starting from 10, directly, like all the answers for these 10 calls are coming at the same time. Is like ASP.NET is only responding to one call only at a time, from the browser. I was having the impression that the server will process at least 2 calls at the same time from the same browser.

Can you help me with this, please?

Please check this article . I think that it will help you with your issue. But also there is an integrated solution in asp.net webforms named as UpdateProgress msdn link with example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM