开始很长一段时间的后台任务 [英] Start a long time background task

查看:101
本文介绍了开始很长一段时间的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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    
}

我该怎么办呢?

推荐答案

您可以通过任务StartNew()方法的一个参数,表明你开始的任务是长期运行,它提供了一个暗示,任务计划开始一个新的线程中的任务。

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.

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

这篇关于开始很长一段时间的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆