views:

273

answers:

3

I'd like to implement a POP3-mailbox processing component for our medium-sized consumer website. Our site uses IPSwitch as the mail/smtp/pop host.

This processing component would let us offer "reply to this message board discussion via email" type services. It would need to run constantly; or at the very least, every 3-5 minutes or something.

I'm pretty sure I know what I need to do to implement this POP3 reader in C#, but I'm just not sure of the mechanism that makes it happen every 3-5 minutes, keeps it running when the machine reboots, etc. Task Scheduler seems a little brittle. Should this be written instead as a Windows Service? And if so, can I get some good pointers on how to write an Installable Service in C#? Or, if there's a better plan to run a daemon on an IIS box, I'd much appreciate it.

A: 

OK, this tutorial on creating a Windows Service in C# appears to be just what I need. Any gotchas to be aware of? Thanks for any advice.

Creating a Windows Service in C#

Steve Murch
Here's another useful tutorial: http://www.aspfree.com/c/a/C-Sharp/Timer-Objects-in-Windows-Services-with-C-sharp-dot-NET/
Steve Murch
Just to close this one out, the right way to go is definitely to use C# to implement a Windows Service. Not as scary as it sounds; the above link makes it pretty clear.
Steve Murch
A: 

On this PC I don't have Visual Studio so I can't get you exact steps, But just after you create your Windows Service Project add an installer to that. I believe you will want to right click on the service and you will see an option to add installer.

What I have done when making a service is to put 100% of my logic in a class that is outside the service. Then I added a Console Program and added a reference so I could easily run the console program (which in turn caused the logic to occur).

Kirk
A: 

You could run a thread on the background which will last as long as your application is alive. So in your global.asax, on Application_Start run something like

Thread  thread = new Thread(new Task());
thread.Start();

and your Task:

private void Task()
{
//do something
Sleep(TimeSpan.FromMinutes(15).ToMilliseconds());
}

Haven't tested the code above myself, but should work I think. The following links shows a tutorial building a simple Scheduler:

http://nayyeri.net/how-to-build-a-task-scheduler-system-for-the-asp-net-part-1

Remember that the task won't be executed when your application isn't running / initialized yet, but i think this is not a problem in your scenario.

Richard