Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could have the Index action (the one that is starting the task) generate an unique number that will be associated to this task (could be a guid) and store an entry into the cache associated to this number. Then return the number to the view.</p> <p>The task will then be running silently in the background and could update the entry you stored into the cache (with information such as the progression of the task or if you cannot implement this simply indicate whether the task has finished or not). Once the task finishes, remove the entry from the cache.</p> <p>The view itself could send AJAX requests at regular intervals to another controller action and pass the id of the task. The action will look for the corresponding entry in the cache using this key and return to the view information about the running task. The view itself could then update the UI.</p> <p>Let's have an example, shall we?</p> <pre><code>public ActionResult Index() { var taskId = Guid.NewGuid().ToString(); var policy = new CacheItemPolicy { Priority = CacheItemPriority.NotRemovable, // Adjust the value to some maximum amount of time that your task might run AbsoluteExpiration = DateTime.Now.AddHours(1) }; MemoryCache.Default.Set(taskId, "running", policy); Task.Factory.StartNew(key =&gt; { // simulate a long running task Thread.Sleep(10000); // the task has finished executing =&gt; we could now remove the entry from the cache. MemoryCache.Default.Remove((string)key); }, taskId); return View((object)taskId); } </code></pre> <p>and then you could have another controller action that will be called by the view with an AJAX call to notify the progression of the task:</p> <pre><code>[HttpPost] public ActionResult TaskProgress(Guid taskId) { var isTaskRunning = MemoryCache.Default.Contains(taskId.ToString()); return Json(new { status = isTaskRunning }); } </code></pre> <p>and finally you could have the Index view:</p> <pre><code>@model string &lt;div id="status"&gt;Task with id @Model has been started and running&lt;/div&gt; &lt;script type="text/javascript"&gt; // start continuous polling at 1s intervals window.setInterval(function() { $.ajax({ url: '@Url.Action("TaskProgress", new { taskId = Model })', type: 'GET', cache: false, success: function(result) { if (!result.status) { // the task has finished executing =&gt; let's notify the user $('#status').html('The task has finished executing'); } } }); }, 1000); &lt;/script&gt; </code></pre> <p>Of course this is just an oversimplified example. In a real world scenario you will have view models, use a complex model for the cache instead of just a simple string where you could hold information about the task and the result of the task if this task need to yield some result after it has finished executing, ...</p>
 

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