mardi 16 juin 2015

Progress Bar using jquery and MVC3

I have a long running process. I created a database table to store status updates and rough completed percentages. The long running process adds status updates to the database as it goes. It's just a basic controller method with lots of stuff in it.

    public ActionResult SaveProjectSiteItems(FormCollection collection)
        //Do long process

I also created a method to check these statuses and update a progress bar.

    public JsonResult GetProgress(int progressBarID)
        this.ControllerContext.HttpContext.Response.AddHeader("cache-control", "no-cache");
            ICOMRepository comRepository = new COMRepository();
            v_ProgressBar vProgressBar = comRepository.GetVProgressBar(progressBarID);

            return Json(new { wasSuccess = true, percent = vProgressBar.Percent, message = vProgressBar.Message });
        catch (Exception ex)
            Log.Error(ex, Request.Path, UserHelper.GetEmail());
            return Json(new { wasSuccessful = false, message = "Error - No progress bar found." });

I use javascript to call this method on a delay.

function progressBarUpdater() {
var data = {
    ProgressBarID: progressBarID
setTimeout(function() {
    jsonGetHelper("/site/progress/GetProgress", data, function(results) {
        if (results.wasSuccess) {
            $("#progressBarModal p").text(results.message);
            $("#progressBarModal #progressBar").css("width", results.percent + "%");
            if (results.Percent != 100) progressBarUpdater();
}, 3000);


Here is where the long process is started in the javascript.

function saveProjectSiteItems() {

$("#projectsiteitemsdialog #ProgressBarID").val(progressBarID);
jsonFormHelperAsync("#projectsiteitemsdialog", "/site/studies/SaveProjectSiteItems", processSaveProjectSiteItems);


It seems that my controller is still locked doing the long process however. In debugging I can see the javascript call the long process, then move on to call the GetProgress method but it doesn't actually get to the controller method until after the long process is complete, which defeats the purpose.

Can anyone tell me how to modify things so that the GetProcess method actually gets called with the long process is still running? I've found some examples, but they all seem to be using local variables to store the progress data, so I'm not sure how to apply it. Is doing things with a database a bad idea? I was thinking it would have a fringe benefit of logging the long task's various stages.

Aucun commentaire:

Enregistrer un commentaire