mvc3 操作之间的线程同步
这是我的代码
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
Thread t1 = new Thread(DoLengthyOperation);
t1.Start();
return View();
}
public ActionResult About()
{
//Check if thread t1 is completed or not, if not keep waiting.
return View();
}
public void DoLengthyOperation()
{
Thread.Sleep(10000);
}
我想做的是
1)在 Home 操作中启动长进程,因为该进程不会返回任何内容,因此没有必要等待
2)在我的“关于”操作中,我想检查进程启动的“主页”操作是否完成,如果没有,则等待其完成。
我尝试过静态实例,但这对同时请求没有帮助,
我也尝试过全局变量,但这并没有帮助,因为每个请求都会获得控制器的新副本。
我的最终目标是当用户查看索引页面时我的漫长过程应该在后台完成,我的过程需要 20 秒。
任何帮助将不胜感激,
谢谢
here is my code
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
Thread t1 = new Thread(DoLengthyOperation);
t1.Start();
return View();
}
public ActionResult About()
{
//Check if thread t1 is completed or not, if not keep waiting.
return View();
}
public void DoLengthyOperation()
{
Thread.Sleep(10000);
}
What i want to do is
1) Start long process in Home action, as that process does not return anything so there is no point waiting
2) in my "About" action I want to check if process started "Home" action is finished or not, if not then wait until its finish.
I have tried static instance but that won’t help with simultaneous request,
I have also tried global variable but that didn’t help as every request gets new copy of controller.
my ultimate goal is when user is looking at index page my long process should finish in the background, my process take 20 sec.
Any help would be appreciated,
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用任务,并将其存储在会话状态字典中。
请注意,如果您不使用进程会话状态,则这将不起作用。
Use a Task, and store it in your session state dictionary.
Note that if your not using in process session state this will not work.