2014-06-12 33 views
1

我正在处理ASP.NET MVC5应用程序中的脚本。这个脚本正在处理15.000个文件,所以我不想等到最后刷新我的视图。在异步任务期间刷新部分视图

我的问题是,我想我的异步任务期间刷新我的看法。

我尝试了许多解决方案,如使用AJAX我的任务期间,我重新加载局部视图,但是当我启动我的脚本,它阻止evrything从不刷新我的观点,直到这个脚本的结尾。

我的代码:

查看

@{ 
    ViewBag.Title = "ConvertScript"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>ConvertScript</h2> 

<div id="PartialDiv"> 
    @{Html.Partial("getStatus");} 
</div> 

@section Scripts 
{ 
    @Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js") 
    <script type="text/javascript"> 
     $(function() { 
      setInterval(function() { $('#PartialDiv').load('/Annotation/Refresh'); }, 1000); // every 3 sec 
     }); 
    </script> 
} 

控制器

public string testOut(int callDuration, out int threadId) 
     { 
      var File = from m in db.Annotations where m.IsDocument == true select m; 
      int i; 
      for (i = 0; i < 100; ++i) 
      { 
       Annotation item = File.OrderBy(t => t.FileSize).Skip(i).Take(1).Single(); 
       using (FileStream stream = new FileStream("C:\\Users\\administrator\\Documents\\Visual Studio 2013\\Projects\\Files\\" + item.FileName, FileMode.Create, FileAccess.ReadWrite)) 
       { 
        byte[] fileContent = Convert.FromBase64String(item.DocumentBody); 
        Debug.Write("Doing item nb : " + i + " Filename : " + item.FileName + "\n"); 
        stream.Write(fileContent, 0, fileContent.Length); 
        stream.Close(); 
        Session["count_files"] = (int)Session["count_files"] + 1; 
       } 
      } 
      threadId = Thread.CurrentThread.ManagedThreadId; 
      return ("Yes"); 
     } 

     public delegate string AsyncMethodCaller(int callDuration, out int threadId); 

     [OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 1)] 
     public ActionResult Refresh() 
     { 
      Debug.Write("Refresh " + Session["State"] + "\n"); 
      if ((int)Session["State"] == 2) 
      { 
       Debug.Write("In\n"); 
       int threadId; 
       AsyncMethodCaller caller = testOut; 
       IAsyncResult result = caller.BeginInvoke(10, out threadId, null, null); 
       string res = caller.EndInvoke(out threadId, result); 
      } 
      Session["State"] = (int)Session["State"] + 1; 
      ViewData["count_file"] = (int)Session["count_files"]; 
      return PartialView("getStatus"); 
     } 

有没有人有一个想法,要做到这一点?

感谢, 奥利弗

回答

0

你可以使用SignalR将数据从异步方法服务器发送到客户端。基本上,SignalR是由微软实施的多种方法(对于几乎所有浏览器都有回退功能)允许进行双向通信,这可能是您正在寻找的。

我建议你学习它。你可以在这里阅读:http://signalr.net/,并且在MVA上有一些关于SignalR的内容。文档是here,它似乎提供了很多教程,让你开始。祝你好运。

+0

感谢您的咨询!你有主要的教程,我可以遵循这些技术?我从他们网站上的基本聊天开始,但如果你有其他好的课程,请发给我;)谢谢你! – Tiekeo