1

I have a particularly long running method that I need to execute from my controller. The Method is in it's own Model. I am using an async controller, and I have the method setup using asyncFunc library to make it asynchronous. I have also tried invoking it on it's own process. The problem is I want to controller to go ahead and return a view so the user can continue doing other things as the method will notify the user it is completed or has any errors via e-mail.

The problem is even thogh it is an asynchronous method the controller will not move forward to return the view until the process is done. 15+ mins. and if you navigate to a different page the method stops trying to execute.

so how can I get the method to execute as a worker and free up the controller?

Any Help would be greatly appreciated.

all the best,

Chase Q, Aucoin

ChaseAucoin
  • 725
  • 1
  • 8
  • 16

3 Answers3

0

Use ThreadPool.QueueUserWorkItem() as a fire-and-forget approach in the ASPX page.

Do the long-running work in the WaitCallback you pass to QUWI. when the work is complete, that WaitCallback can send an email, or whatever it wants.

You need to take care to handle the case that the w3wp.exe is stopped during the 15 minute run. What will you do if the work is 2/3 complete? Some options are, making the work restartable, or just allowing the interrupted work to be forgotten.

Making it restartable might mean, when w3wp.exe restarts, your ASP.NET logic makes sure to begin again, any work that was interrupted. It might mean that your ASP.NET logic sets "syncpoints" so that it knows where to restart.

If you want the restartable option, you might think about Workflow, which is specifically designed for this purpose - maintaining state of long-running workflows, restarting automatically, and so on. If you use Workflow, you can set it to run asynchronously, and you may decide you do not need QueueUserWorkItem.

see also:
Moving a time taking process away from my asp.net application
the Workflow Foundation tag

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • Thank you very much for the feedback I will read the information you have provided, but on face value looks like it should help. – ChaseAucoin May 01 '11 at 16:58
0

This will help > http://msdn.microsoft.com/en-us/library/ms227433.aspx

It is the standard way of running a background process on the server in the .NET stack.

neebz
  • 11,465
  • 7
  • 47
  • 64
0

I don't know why, but I still live in conviction that this should not be done. Executing background threads in ASP.NET smells. You will also steal threads from ASP.NET thread pool which is controlled by IIS. It can decide that something is wrong with your worker process and restart it any time just to keep memory consumption, processing time consumption or thread consumption low. If you need background logic create custom NT service and call the process on that service either via old .NET remoting or WCF.

Btw. approach I described is used frequently in commercial applications and those which doesn't use it often self-host the whole web server.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670