Im building my first Windows Service. It's a component that connects to a mailbox and downloads all mails and store them on my local drive.
My questions are these.
What is the best way to repeat a program call in c# windows service. Im thinking of using a simple timer? Where do I start and stop the timer? is it in the service itself or the program my service is running?
What code should be included in a Windows Service for the following function
protected override void OnStart(string[] args)
{
//timer?
// MyProgram mp = new MyProgram();
}
Should I simply start my application with a new instance like above or should I include more stuff?
As I said this is my first time for using Windows Services so.
EDIT:
Thanks for all the answers. There are of course lots of different ways to do this but I found that best way for me is the one mark as a solution.
Thanks for any help!
I use AutoResetEvent with a timeout:
And when I want to stop, I simply set on the
autoResetEvent
.Timer is bad for multiple reason one being Reentry.
OnStart(string[] args)
will create theautoResetEvent
.Here is my implementaion of it as
PollingWorker
:Here's a template you can use it handles the reentrantcy problems with using a timer.
OnContinue OnPause and OnStop are petty easy to work out.
Create a thread or something, no need for a timer! In the OnStart you start the thread and in OnStop you can stop the thread.