1

I am trying to get the progress of a controller method. I have used setInterval method to call the progress method inside the controller but the ajax call inside the setInterval only hits the controller after the execution of controller method and till that time the ajax requests status keeps pending on browser network tab.

I have considered making first method asynchronous

Java Script code

function Import() {
    $('.loader').show();

    var checked;
    if ($('#chkNewCycle').is(":checked")) {
        checked = 'on';
    }
    else {
        checked = 'off';
    }

    var NewCycle = { IsNewCycle: checked };

    $.ajax({
        url: '/DataImport/Import',
        type: 'POST',
        data: NewCycle,
        success: function (result) {
            $('.loader').hide();
            if (result == "success") {
                window.clearInterval(intervalId);
                alert("Imported file for processing.");
                ClearData();

            }
            else {
                alert(result);
            }
        }
    });   


    var intervalId = window.setInterval(function () {
        $.getJSON('/DataImport/GetProgress', function (json) {
            console.log(json.Progress);
            $('#progress').html(json.Progress + '%');
        });
    }, 2000);



}

controller.cs

public async Task<string> Import(string IsNewCycle)
{
/*Some Code*/

await Task.Run(() => { 
/*Some Code*/

int iProgress =Convert.ToInt32(dProgress * 100);
HttpContext.Application["Progress" ] = iProgress;

 });

return string;


}

public ActionResult GetProgress()
{
return Json(new
{
     Progress = HttpContext.Application["Progress"]
}, JsonRequestBehavior.AllowGet);
}

I need to get both the controller method hit simultaneously

6
  • I think this should work! How are you determining that it is not hitting both simultaneously Commented Nov 4, 2019 at 10:14
  • Inside Chrome Network tab multiple progress request status is pending Commented Nov 4, 2019 at 10:17
  • are you holding the debugger at your server side? Commented Nov 4, 2019 at 10:18
  • yes and its not hitting. It will hit once the first ajax call gets completed Commented Nov 4, 2019 at 10:19
  • if you'll hold the debugger it won't hit another. Try using log of manipulating some data. and see if changed? Commented Nov 4, 2019 at 10:20

1 Answer 1

2

Decorate your controller with [SessionState(SessionStateBehavior.ReadOnly)] attribute, then you can execute requests in parallel.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.