开发者

Don't wait for longrunning operations ASP.NET MVC

开发者 https://www.devze.com 2023-02-13 21:37 出处:网络
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 q

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);
    }


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.


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.


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 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消