PDA

Click to See Complete Forum and Search --> : [1.0/1.1] Stop Thread Continued


steve65
Oct 13th, 2006, 11:38 AM
Hi all,

Thank you for all of your help on getting me this far. My service is coming along fine and I now what it to stop if there is a problem. In my OnStart method I start my thread for the service. To stop a service it was suggested that I set a flag and have the thread test for it.

In my thread I initiate a timer object to run my process at the desired interval. If my process fails I set a flag in the class. The next time the timer runs it checks this flag and if the process failed the timer is disabled. While I am in the OnServiceElapsedTime method is there a way to get the thread to check that the timer has been deactivated and that the thread should close?

Thanks for the advice.
Steve

namespace CoReplication
{
public class Replication : System.ServiceProcess.ServiceBase
{
public Replication()
{
}

static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Replication() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

private void OnServiceElapsedTime(object source, System.Timers.ElapsedEventArgs e)
if (_replicationMain.Failed() == false)
{
_replicationMain.Start();
}
else
{
_serviceTimer.Enabled = false;
}

protected override void OnStart(string[] args)
{
Thread t = new Thread(new ThreadStart(StartService));
t.Start();
}

protected override void OnStop()
{
StopService();
}

private void StartService()
{
_serviceTimer.Enabled = true;
}

private void StopService()
{
_replicationMain.Stop();
}
}
}