mvc3 操作之间的线程同步

发布于 2025-01-08 22:07:04 字数 692 浏览 2 评论 0原文

这是我的代码

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的那么颓废 2025-01-15 22:07:04

使用任务,并将其存储在会话状态字典中。

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    var longRunningTask = Task.Factory.StartNew( DoLengthyOperation );
    Session["MyApp_SessionTask"] = longRunningTask;
    return View();
}


public ActionResult About()
{
    // Note that the below Wait method has a bunch of overloads you can use
    // i.e. only wait up until a fixed timeout, wait until a separate cancellation
    // token is signaled, etc.
    var longRunningTask = (Task) Session["MyApp_SessionTask"];
    if ( longRunningTask != null )
        longRunningTask.Wait();
    return View();
}

请注意,如果您不使用进程会话状态,则这将不起作用。

Use a Task, and store it in your session state dictionary.

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    var longRunningTask = Task.Factory.StartNew( DoLengthyOperation );
    Session["MyApp_SessionTask"] = longRunningTask;
    return View();
}


public ActionResult About()
{
    // Note that the below Wait method has a bunch of overloads you can use
    // i.e. only wait up until a fixed timeout, wait until a separate cancellation
    // token is signaled, etc.
    var longRunningTask = (Task) Session["MyApp_SessionTask"];
    if ( longRunningTask != null )
        longRunningTask.Wait();
    return View();
}

Note that if your not using in process session state this will not work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文