不要等待长时间运行的操作 ASP.NET MVC

发布于 2024-10-19 14:00:54 字数 381 浏览 1 评论 0原文

当用户执行某些操作(生成多个报告)时,我们会触发一些长时间运行的操作。我们不希望用户等到这些报告生成。有没有一种无需实施作业调度程序即可快速完成此任务的方法?也许使用这样的线程?或者这不安全?

    public ActionResult About()
    {
        Thread thread = new Thread(new ThreadStart(MuchWork));
        thread.Start();
        return View();
    }

    public void MuchWork()
    {
        Thread.Sleep(10000);

        Thread.Sleep(4000);
    }

We have some long running operation firing when the user does something(generating several reports). We don't want the user to wait till those reports have been generated. Is there a quick way to accomplish this without having to implement a jobscheduler? Maybe using threads like this? Or is this not safe?

    public ActionResult About()
    {
        Thread thread = new Thread(new ThreadStart(MuchWork));
        thread.Start();
        return View();
    }

    public void MuchWork()
    {
        Thread.Sleep(10000);

        Thread.Sleep(4000);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

赠意 2024-10-26 14:00:54

请参阅帖子 The Dangers of Implementing Recurring Back Tasks In ASP.NET by Phil Haack,其中描述了如何在使用 IRegisteredObject 接口 限制 IIS/AppPool 关闭问题。

See the post The Dangers of Implementing Recurring Background Tasks In ASP.NET by Phil Haack where he describes how to hack a psuedo-job scheduler while using the IRegisteredObject interface to limit IIS/AppPool shut-down issues.

听风吹 2024-10-26 14:00:54

这是不安全的。您的服务器在操作过程中崩溃了怎么办?并且用户不知道该操作从未被执行。

一般来说,工作流/持久机制会对此有所帮助,但您必须真正考虑灾难性场景的业务/用户体验案例。

It is not safe. What about your server crashing during the operation? And the user not knowing the operation never was performed.

In general, a workflow / persistent mechanism would help with that, but you have to really think about your business / user experience case for catastrphic scenarios here.

林空鹿饮溪 2024-10-26 14:00:54

也许使用线程池或 TPL,这样您就可以检查状态并且不会被线程滥用?
我所说的“检查”是指在旋转另一个请求之前查看您已经运行了多少个请求。

我猜测操作的结果被写入某个数据库表中,以便用户可以轮询并查看状态?

maybe use the threadpool, or TPL so you can check status and not get thread-abused?
by 'check' i mean see how many of these requests you already have running before spinning another one.

im geussing that the result of the operation is written in some DB table , so the user can poll and see the status ?

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