Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing methods into a Task to avoid blocking the asp.net thread

Tags:

c#

asp.net

task

I'm wondering if the following code has any gotcha's that I'm not aware of when running on a webserver. Reading through the excellent series http://reedcopsey.com/series/parallelism-in-net4/ I am unable to find anything that relates specifically to my question, same with the msdn, so I thought I'd bring it here.

Example call:

public ActionResult Index() {
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    Task.Factory.StartNew(() => {
        //This is some long completing task that I don't care about
        //Say logging to the database or updating certain information
        System.Threading.Thread.Sleep(10000);
    });

    return View();
}
like image 765
Buildstarted Avatar asked May 02 '11 19:05

Buildstarted


People also ask

Does Task create new thread?

Note: Just using a Task in . NET code does not mean there are separate new threads involved. Generally when using Task. Run() or similar constructs, a task runs on a separate thread (mostly a managed thread-pool one), managed by the .

Does Task run Use the ThreadPool?

By default, TPL types like Task and Task<TResult> use thread pool threads to run tasks. You can also use the thread pool by calling ThreadPool.

What is difference between Task and thread in C#?

Differences Between Task And ThreadThe Thread class is used for creating and manipulating a thread in Windows. A Task represents some asynchronous operation and is part of the Task Parallel Library, a set of APIs for running tasks asynchronously and in parallel. The task can return a result.

When using a thread pool What happens to a given thread after it finishes its Task?

And it continues showing load after 75% of the threads have completed their job. If 75% of the 500 threads have completed their job then that leaves 100+ threads that continue to run.


2 Answers

ASP.Net supports asynchronous pages, see Asynchronous Pages in ASP.NET, but is a complicated programming model and does not bind at all with MVC. That being said, launching asynchronous tasks from a synchronous requests handler works up to a point:

  • if the rate at which requests add new tasks exceeds the average rate of processing your process will crash eventually. The Tasks take up live memory and eventually they will fill up the in memory queues where they're stored and your will start getting failures to submit.
  • .Net Taks are inherently unreliable as they lack a persistent storage, so all tasks that are submitted async must be threaded as 'abandonware', ie. if they never complete there is no loss to the application nor to the user making the request. If the task is important, then it must be submitted through a reliable mechanism that guarantees execution in the presence of failures, like the one presented in Asynchronous procedure execution.
like image 164
Remus Rusanu Avatar answered Sep 19 '22 04:09

Remus Rusanu


One important thing in this case is to ensure that the code contained inside the task is wrapped in a try/catch block or any possible exceptions thrown in this thread will propagate. You also should ensure that in this long running task you are not accessing any of the Http Context members such as Request, Response, Session, ... as they might no longer be available by the time you access them.

like image 30
Darin Dimitrov Avatar answered Sep 18 '22 04:09

Darin Dimitrov