tags:

views:

1219

answers:

6

E.g. we this code in the asp.net form codebihind:

private void btnSendEmails_OnClick()
{
    Send100000EmailsAndWaitForReplies();
}

This code execution will be killed by the timeout reason. For resolving the problem I'd like to see something like this:

private void btnSendEmails_OnClick()
{
    var taskId = AsyncTask.Run( () =>  Send100000EmailsAndWaitForReplies() );
    // Store taskId for future task execution status checking.
}

And this method will be executed for some way outside the w3wp.exe process within a special enveronment.

Does anybody know a framework/toolset for resolving this kind of issues?

Update: The emails sending method is only an example of what I mean. In fact, I could have a lot of functionality need to be executed outside the asp.net working process.

E.g. this point is very important for an application which aggregates data from a couple of 3rd party services, do something with it and send it back to another service.

A: 

One option is to have the task execute a certain amount of emails, then Response.Redirect back to itself and repeat until all of your emails have been sent.

madcolor
A: 

I can think of two possible paths I'd head down:

  1. You could create a windows service that hosted a remoted object and have your web app call that remoted object to ensure that the method executed outside of the IIS process space.
  2. You could set up a DB or MSMQ to which you would log a request. Your web app could then monitor the status of the request to subsequently notify the user of it's completion. I would envision a service completeing the requests.
JPrescottSanders
A: 

Server.ScriptTimeout = 360000000;

Mike
A: 

You could have the functionality that sends the mails run as a service. Submit the request to it, and let it process it. Query every once in a while for its status. If you have control of the server you can install a windows service which would probably be ideal for optimal processing and lifetime management.

mattlant
+1  A: 

ASP.NET 2.0+ supports the concept of Asynchronous pages. Add the page directive Async="true" to your page.

Then in Page_Load, use the BeginEventHandler and EndEventHandler delegates to have code executed asynchronously in the corresponding "handlers".

<%@ Page Language="C#" Async="true" %>
<script runat="server">

  protected void Page_Load(object sender, EventArgs e)
  {
    BeginEventHandler begin = new BeginEventHandler(BeginMethod);
    EndEventHandler  end = new EndEventHandler(EndMethod);

    AddOnPreRenderCompleteAsync(begin, end);
  }
</script>
Ash
+2  A: 

This has been discussed as a part of other questions:

http://stackoverflow.com/questions/50221/multithreading-in-aspnet#50377

http://stackoverflow.com/questions/57845/backgroundworker-thread-in-aspnet

There is no good way to do this. You don't want any long running processes in the ASP.NET worker process, since it might recycle before you are done.

Write a Windows Service to run in the background that does the work for you. Drop messages into MSMQ to initiate tasks. Then they can run as long as they want.

Eric Z Beard