Reputation: 65
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
Upvotes: 1
Views: 300
Reputation: 667
Decorate your controller with [SessionState(SessionStateBehavior.ReadOnly)]
attribute, then you can execute requests in parallel.
Upvotes: 2