Async Controllers (MVC), long running process with “stops”

后端 未结 3 1490
感情败类
感情败类 2021-01-14 03:06

I\'m interested in running a long process, I want to update the UI as soon as results start coming in and not wait for it to finish.

How would I go about this? I\'ve

3条回答
  •  粉色の甜心
    2021-01-14 03:52

    This article seems to describe what you want, simple and w/o SignalR:

    ASP.NET MVC 3: Async jQuery progress indicator for long running tasks

    Controller:

    public class HomeController : Controller { private static IDictionary tasks = new Dictionary();

     public ActionResult Index()
     {
       return View();
     }
    
     public ActionResult Start()
     {
       var taskId = Guid.NewGuid();
       tasks.Add(taskId, 0);
    
       Task.Factory.StartNew(() =>
       {
         for (var i = 0; i <= 100; i++)
         {
           tasks[taskId] = i; // update task progress
           Thread.Sleep(50); // simulate long running operation
         }
         tasks.Remove(taskId);
       });
    
       return Json(taskId);
     }
    
     public ActionResult Progress(Guid id)
     {
       return Json(tasks.Keys.Contains(id) ? tasks[id] : 100);
     }
    

    }

    View:

    
    
                                     
                  
提交回复
热议问题