Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a long time background task

Tags:

c#

asp.net-mvc

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?

like image 921
Neir0 Avatar asked Mar 13 '12 12:03

Neir0


People also ask

What is a long running background task?

A long running task is a task that requires more than 30 seconds to complete and involves a large amount of data.

How do you handle long running background tasks?

Scheduling deferred work through WorkManager is the best way to handle tasks that don't need to run immediately but which ought to remain scheduled when the app closes or the device restarts.

What is the meaning of background tasks?

A background task is a task performed by a system during the time when its primary application is idle.


1 Answers

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);
like image 89
medkg15 Avatar answered Oct 01 '22 17:10

medkg15