Note that there are some explanatory texts on larger screens.

plurals
  1. POAsync jQuery database loop - Help on implementing progress bar
    primarykey
    data
    text
    <p>So I am trying to add an async progress bar on a really slow and long query that inserts a bunch of rows to a database. My implementation is based off this example: <a href="http://blog.janjonas.net/2012-01-02/asp_net-mvc_3-async-jquery-progress-indicator-long-running-tasks" rel="nofollow">http://blog.janjonas.net/2012-01-02/asp_net-mvc_3-async-jquery-progress-indicator-long-running-tasks</a></p> <p>Here is the javascript code for the progress bar</p> <pre><code>function updateMonitor(taskId, status) { $("#monitors").html("Task [" + taskId + "]: " + status); } //other code if (doSend == true) { $.post("/SendBatch/HandleBatchRequest", { //other code }, function (taskId) { // Init monitors //breakpoint here only stops it once the "/SendBatch/HandleBatchRequest" is done. PROBLEM. $("#monitors").append($("&lt;p id='" + taskId + "'/&gt;")); updateMonitor(taskId, "Started"); // Periodically update monitors var intervalId = setInterval(function () { $.post("SendBatch/Progress", { id: taskId }, function (progress) { if (progress &gt;= 100) { updateMonitor(taskId, "Completed"); clearInterval(intervalId); } else { updateMonitor(taskId, progress + "%"); } }); }, 100); } ,"html"); </code></pre> <p>Then there is the DIV within the display part of the website</p> <pre><code>&lt;div id="monitors"&gt;&lt;/div&gt; </code></pre> <p>Here is how the controller looks</p> <pre><code>public SendBatchController //some code private static IDictionary&lt;Guid, int&gt; tasks = new Dictionary&lt;Guid, int&gt;(); public ActionResult HandleBatchRequest( //some code ) { var taskId = Guid.NewGuid(); tasks.Add(taskId, 0); var batchId = Guid.NewGuid().ToString("N"); var costd = cost.ToDecimal(); IEnumerable&lt;BatchListModel&gt; customers; try { customers = new CustomerService(_customerRepository.Session).GetCustomers( //some code ); } catch (Exception err) { return Json(err.Message); } if (doSend) { var sent = 0; foreach (var c in customers) { try { var usr = _customerRepository.LoadByID(c.ID); var message = new ComLog { //insertions to log }; _comLogRepository.Save(message); sent++; //progress bar part inside here that is important comes here: tasks[taskId] = sent; } catch (Exception e) { Log.WriteLine("ERR:" + e); } tasks.Remove(taskId); } return Json(taskId); } return Json(customers.Count() + " customers"); } public ActionResult Progress(Guid id) { return Json(tasks.Keys.Contains(id) ? tasks[id] : 100); } </code></pre> <p>This does not work. The process works in the background. The DIV does show up after the query is done with a "Complete" message. It does not show during the query. I am not sure how async stuff works this works so there might be some logic error here. For instance, does it even know how many customers there is in total? Another key part of the code is the "tasks[taskId] = sent;" wich I am not sure is done correctly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload