开始长时间的后台任务

Nei*_*ir0 13 c# asp.net-mvc

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

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

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

    //start task async
    task.start(); 

    return View();
}

public void Stuff()
{
    //long time operation    
}
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

med*_*g15 19

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

var task = Task.Factory.StartNew(Stuff, TaskCreationOptions.LongRunning);
Run Code Online (Sandbox Code Playgroud)