Personnaly, I would go this way:
**In your Service Class:
protected override void OnStart(string[] args)
{
// Define working directory (For a service, this is set to System)
Process pc = Process.GetCurrentProcess();
Directory.SetCurrentDirectory (pc.MainModule.FileName.Substring
(0,pc.MainModule.FileName.LastIndexOf(@"\")));
// Start the Worker thread
Thread WorkerThread = new Thread(new ThreadStart(m_Worker.DoWork));
WorkerThread.Start();
}
protected override void OnStop()
{
// Ask the Reception thread to stop receiving
m_Worker.Stop();
}
....
private Worker m_Worker;
*****
And then have a Worker class where you do your stuff.
The DoWork() will be the code to execute in your service. It will wait for
a termination request provided by the Service itself through Worker.Stop()
which can
either set an event flag or a local flag to true.
eg:
public void Stop()
{
// In case the thread is running, ask to stop and wait for cleanup
m_bMustStop = true;
if (m_Thread != null)
{
// Wait up to 25 s for the communication to terminate
if (!m_Thread.Join(25000))
{
// Thread was not able to terminate within given time
}
}
}
public void DoWork()
{
// Initialize some thread parameters
m_Thread = Thread.CurrentThread;
m_Thread.Name = "Whatever you want;
...
while (!m_bMustStop)
{
// do your stuff
// Sleep a while (eg. 10s)
Thread.Sleep(10000);
}
}
private Thread m_Thread = null;
"CG" <an*******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
No.
I start a thread in the elapsed event for my timer.