Reset Windows Service Programatically

C

Ciaran

Hi All,

I have created a Windows Service. In the event of an Exception
occuring I would like to notify Windows to restart the service
as shown below:

<snip>

protected override void OnStart(string[] args)
{
ThreadStart starter = new ThreadStart(this.StartListening);
Thread threadWorker = new Thread(starter);

threadWorker.Start();
}

public void StartListening()
{
try
{
...
}
catch (Exception)
{}
}

<snip>

I'm having no joy with this...any ideas are appreciated :blush:)
 
J

Jeffrey Wynn

Hello,

Look into the use of the ServiceController class.

http://msdn.microsoft.com/library/d...eatingServiceControllerComponentInstances.asp

Another way of approaching the problem is to create another thread in your
service whose job is to monitor the health of your threadWorker. If that
thread notices an error via some flag, monitoring the thread's IsAlive
property, or is signalled by the theadWorker, it can abort the threadWorker
if necessary and restart it.

Hope this helps.
--
Jeffrey Wynn
(e-mail address removed)
Replace -no-spa-m with "-op-to-nl-ine" and remove the dashes


Ciaran said:
Hi All,

I have created a Windows Service. In the event of an Exception
occuring I would like to notify Windows to restart the service
as shown below:

<snip>

protected override void OnStart(string[] args)
{
ThreadStart starter = new ThreadStart(this.StartListening);
Thread threadWorker = new Thread(starter);

threadWorker.Start();
}

public void StartListening()
{
try
{
...
}
catch (Exception)
{}
}

<snip>

I'm having no joy with this...any ideas are appreciated :blush:)
 

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