Execute method asynchronously in asp.net

I have a web page with a button that generates some files on the server path. (It takes 5 to 20 minutes). I want to create an async task that will keep running even after the user closes the browser. Can this be done with asp.net 4 and C #?

+3


source to share


6 answers


Actually, it is recommended that you do not do this. Instead, we recommend that you create a service (such as a Windows service) that does asynchronous processing. In your web application, you create methods that start this process and another method that will poll the service to determine if processing has completed.



There are several reasons for this, but one of the biggest is that the standard and recommended configuration for web servers allows the server to kill long requests.

+1


source


You have no control over the thread pool in an asp.net application. You can't even guarantee that the request will complete on the same thread it started with. Thread creation uses the same application pool that the web server uses, and you can use all request threads, leaving the web server unavailable to process requests.

You should implement a Windows service that hosts a WCF service that you can call from your web application. In this service, you can start a thread to handle a long process. At the end of this process, you can update a status flag (for example, from processing to complete), which the user can view to determine if the files have been processed.



I would recommend using Topshelf to implement your windows service, it will save you a lot of headaches.

+1


source


Or I didn't understand what you want to do, or that you don't need to do anything.

After the request is submitted, the request process continues regardless of whether the user's browser has been closed or not. You don't have to do anything

The dreamlike nature of stateless applications without registration ...

0


source


you need to create a thread that does a long running task

see below:

http://kiranpatils.wordpress.com/2010/01/01/performing-a-long-running-task-with-asp-net/

In any case, whatever you run on the server will continue to work even if the user closes the browser (until you recycle the application or restart the web server).

0


source


Creating a new thread / using a thread pool is the easiest way to create startup tasks.

Note that there is no guarantee that the process will stay alive during a long running task - be prepared for a partial, partial and manual reboot. That is, Restarting AppPoll due to a configuration change.

0


source


The easiest way is to put a task on the ThreadPool. The thread pool threads will survive even after the web page finishes. The code will look like this:

/* Beginning Method */
object someData = new object();
ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessAsync), someData);
/* Ending Method */

static void ProcessAsync(Object stateInfo) 
{
    string dataToString = stateInfo.ToString();
}

      

0


source







All Articles