Windows Service (Stopping and Starting)

G

Guest

Hi

I have developed a Windows Service

When I try to start the Service it tells me that it cannot start as there may not be any work to do
When I comment out below line of code in my OnStart event....
objEventLog.EnableRaisingEvents = Tru
....It starts fin

I have the same issue when stopping the service because of below line of code in my OnStop even
objEventLog.EnableRaisingEvents = Fals

I am using .NET 2003 and I have declared objEventLog as..
Friend WithEvents objEventLog As System.Diagnostics.EventLo

Does anyone have any idea as to why this is causing an issue when starting and stopping the service

Thanks
C
 
J

José Joye

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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top