开始长时间的后台任务

时间:2022-05-06 01:24:22

User request some page at my website.

用户在我的网站上请求一些页面。

What i want to do? Send to user a fast answer and start background task which take a long time. It looks like:

我想做的事?向用户发送快速回答并启动后台任务需要很长时间。看起来像:

public ActionResult index()
{
    var task = new Task(Stuff);

    //start task async
    task.start(); 

    return View();
}

public void Stuff()
{
    //long time operation    
}

How can i do it?

我该怎么做?

3 个解决方案

#1


18  

You can pass the Task StartNew() method a parameter that indicates the task you're starting is "long running", which provides a hint to the Task Scheduler to start the task on a new thread.

您可以向Task StartNew()方法传递一个参数,该参数指示您正在启动的任务是“长时间运行”,这会向任务计划程序提供一个提示,以便在新线程上启动任务。

var task = Task.Factory.StartNew(Stuff, TaskCreationOptions.LongRunning);

#2


4  

Here's an example from MSDN http://msdn.microsoft.com/en-us/library/ms978607.aspx#diforwc-ap02_plag_howtomultithread

以下是来自MSDN的示例http://msdn.microsoft.com/en-us/library/ms978607.aspx#diforwc-ap02_plag_howtomultithread

While you need it for ASP.NET MVC, you can use the core idea of this ASP.NET WebForms example.

虽然您需要ASP.NET MVC,但您可以使用此ASP.NET WebForms示例的核心思想。

The approach is create a worker thread to do your job, start it and send a "waiting" page to the user. This "waiting" page will refresh at every N seconds, looking for the results processed by the worker thread. When it finishes, the "waiting" page shows the results.

该方法是创建一个工作线程来完成您的工作,启动它并向用户发送“等待”页面。此“等待”页面将每隔N秒刷新一次,查找工作线程处理的结果。完成后,“等待”页面显示结果。

#3


0  

you want to define a delegate and fire it off before returning. See, for example: http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

你想要定义一个委托并在返回之前将其关闭。例如,请参阅:http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

#1


18  

You can pass the Task StartNew() method a parameter that indicates the task you're starting is "long running", which provides a hint to the Task Scheduler to start the task on a new thread.

您可以向Task StartNew()方法传递一个参数,该参数指示您正在启动的任务是“长时间运行”,这会向任务计划程序提供一个提示,以便在新线程上启动任务。

var task = Task.Factory.StartNew(Stuff, TaskCreationOptions.LongRunning);

#2


4  

Here's an example from MSDN http://msdn.microsoft.com/en-us/library/ms978607.aspx#diforwc-ap02_plag_howtomultithread

以下是来自MSDN的示例http://msdn.microsoft.com/en-us/library/ms978607.aspx#diforwc-ap02_plag_howtomultithread

While you need it for ASP.NET MVC, you can use the core idea of this ASP.NET WebForms example.

虽然您需要ASP.NET MVC,但您可以使用此ASP.NET WebForms示例的核心思想。

The approach is create a worker thread to do your job, start it and send a "waiting" page to the user. This "waiting" page will refresh at every N seconds, looking for the results processed by the worker thread. When it finishes, the "waiting" page shows the results.

该方法是创建一个工作线程来完成您的工作,启动它并向用户发送“等待”页面。此“等待”页面将每隔N秒刷新一次,查找工作线程处理的结果。完成后,“等待”页面显示结果。

#3


0  

you want to define a delegate and fire it off before returning. See, for example: http://msdn.microsoft.com/en-us/magazine/cc301332.aspx

你想要定义一个委托并在返回之前将其关闭。例如,请参阅:http://msdn.microsoft.com/en-us/magazine/cc301332.aspx