Hi all,
This is some kind of philosophy question. What is the correct way to build windows service
And what exactly do I mean
My WS (windows service) have timer and every 1 minute it execute method that need to do something. the method need to run on different thread, and it can be stopped during executing.
So, Whet is the correct way using timer thread timer timespan and wait and how can I stop executing

The correct way to Windows service
Kevin8264
Hi
You can use a timer or wait on an event object in the other thread.
So it might be like WaitForSingleObject(someEvent, 600000);
you might use 2 event objects - one for waking up and doing work and the other for terminating the thread:-
AutoResetEvent doworkEvent; AutoResetEvent terminateEvent; public void DoWork() { while (true) { DoYourWork(); AutoResetEvent[] waitEvents = new AutoResetEvent[] {doworkEvent, terminateEvent }; int return = WaitHandle.WaitAny(waitEvents, 1 hour, false); if (return == WaitHandle.WaitTimeout || return == 0) { continue; } else if (return == 1) { break; } else { } } }Regards
Vivek Ragunathan
IB00
Let me try explane again my problem:
My service is running just fine. I can stop it (I'm using system.Timer - As RAM say), and while it not on the executing section every thing is just fine.
Where is my problem in the executing secion. inside the executing method I create new class and do some action (include IO, XML parsing etc). It taken about 2-3 minutes from the start till the end of the executing. I need to stop the service inside that section. and when I stop it, no Disposed is called (and I need to do some action while it stop). This is my problem. How to call Dispose method when the user press stop.
jdang
pinoyz
daimaku
Jstn254473
MrZkitten
rapperux
Inside DoWork() I want to call a new class. Does Thread.Abord() will call the class dispose if not, how can I call it
Alex Farber
enric vives
Why don't you go for a System.Timer You could provide the method that you want to be executed at regular intervals as the handler for the 'Elapsed' event of the timer object. You could set the 'Interval' property to 60000. The 'Elapsed' event will be raised for every expiry of the 'Interval' you specified ('AutoReset' property of the Timer object is 'true' by default). When the user stops the service, just invoke 'Stop()' method on the timer object. This should stop raising the 'Elapsed' event & you should be able to stop the service immediately! This, I believe, is a cleaner & less complex implementation.
Dédé
Osiris43
I want him to wait up to 20 seconds. I can sleep for 10-20 seconds each time (using Span I can wake my service, check if I need to do somehting, and go back to sleep - if not). But again, What to do inside the thrad action. How can I stop it
jdang
Here's an example:
namespace WindowsService1
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}
Thread thread;
protected override void OnStart(string[] args)
{
thread = new Thread(this.DoWork);
thread.Start();
}
protected override void OnStop()
{
thread.Abort();
}
public void DoWork()
{
while (true)
{
DoYourWork();
Thread.Sleep(60000);
}
}
}
}
Greg Beech