473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Programmatically stopping a Windows Service

I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.public.dotnet.languages.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceController framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN
Nov 15 '05 #1
4 15839
Keith,

Have you tried just throwing an exception? Hopefully, the framework
will catch that and shut down the service, and indicate somehow that it was
an error that did it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Keith" <sf*****@yahoo.com> wrote in message
news:d1**************************@posting.google.c om...
I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.public.dotnet.languages.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1
I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceController framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #2
Can you define "fail properly"? I've had problems in the
past if unhandled exceptions occur in a windows service.
These errors cause it to hang when I attempt to "stop"
the service through the management console. However,
when I've added good error catching I haven't noticed
this problem.

Can you do the following:

1. Catch the exception
2. Write the critical error to the event log or
similarly useful location
3. Stop the service

If you're unable to stop the service from within itself,
perhaps you could spawn another process to do so...

Jerry Negrelli
Senior Software Engineer
Data Scientific Corporation
-----Original Message-----
I'm in the same boat as the fellow who posted this message back inAugust:

Title : Windows Service, How does one make a service "fail" properly?Author : Ross Bennett
Group : microsoft.public.dotnet.languages.csharp
URL : http://groups.google.com/groups? q=ross+bennett+group:microsoft.public.dotnet.langu ages.csh
arp&hl=en&lr=&ie=UTF-8&oe=UTF-
8&group=microsoft.public.dotnet.languages.csharp&s elm=%
23hTHL91aDHA.2328%40TK2MSFTNGP09.phx.gbl&rnum=1
I need to find a way to cause the service to stop when it encounters afatal error during processing. Using the ServiceController frameworkclass is not an option, as doing so makes the stop appear to be"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solutionto this and have found nothing. Hard to believe that there are onlytwo of us out here who need to make a Windows Service behave this way.
Thanks
SFN
.

Nov 15 '05 #3
Joy
we faced a similar issue and this is what we did.
Scenario 1 : Service is up and running and because of an application error we wanted to fail the service, so that SCM will
bring it up as per the settings in the recovery tab of the service
properties.

Environment.Exit(-1); will indicate to the SCM that the service
failed and it will restart the service.

Scenario 2 : During startup of the service, a critical resource is
not available and have to stop the service.

================================================== ===========================

private System.Timers.Timer timerStopCurrentService = null;
private MyService()
{
....
timerStopCurrentService = new System.Timers.Timer(1000);
timerStopCurrentService.Enabled = false;
timerStopCurrentService.Elapsed += new
System.Timers.ElapsedEventHandler(timerStopCurrent Service_Elapsed);
....
}

private void timerStopCurrentService_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
StopCurrentService();
}

public void ShutdownService()
{
ServiceController ourService = new ServiceController("ServiceName");
if( ourService.Status == ServiceControllerStatus.Running)
{
StopCurrentService();
}
else
{
timerStopCurrentService.Start();
}
}

private void StopCurrentService()
{
ServiceController ourService = new ServiceController("ServiceName");
ourService.Stop();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show("Error Mesage", "Application Name",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
================================================== ===========================

Regards, Joy

sf*****@yahoo.com (Keith) wrote in message news:<d1**************************@posting.google. com>... I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.public.dotnet.languages.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceController framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #4
Thanks everyone for the replies.

My situation is very similar to the initial posting by Ross Bennett.

I have a service application. The actual work performed by the service
is done in a thread that is created by the main service thread. A
separate class is used to provide the ThreadStart delegate for the
thread. Starting, stopping, pausing, and continuing the service do not
present any problems. It's not an initialization problem.

The difficulty I was experiencing was in the cases where the worker
thread experiences a condition where it no longer makes sense for the
service to continue operation. An example being chronic problems with
network connections, etc.. I could not find a clean way to stop the
service so that the Windows would recognize that the service failed
and allow the recovery options in the Service Control Manager restart
the service again after a period of time.

Using the ServiceController class to stop the service gives the
appearance that all is well and that the service merely stopped due to
a request and not necessarily due to an error condition. Throwing an
exception from the worker thread isn't going to help, as the main
service thread will continue along happily.

I've finally settled on having the class associated with the worker
thread expose an event. The main service thread registers for the
event. The method designated by the service thread issues a call to
Environment.Exit. When the worker thread experiences a fatal error,
the event is fired, the service thread waits for the worker thread to
die, and calls Environment.Exit.

This is seen by Windows as a service failure and makes it eligible for
the recovery options in the Service Control Manager.

Thanks again for everyone's ideas.

jo*****@yahoo.com (Joy) wrote in message news:<2b**************************@posting.google. com>...
we faced a similar issue and this is what we did.
Scenario 1 : Service is up and running and because of an application error we wanted to fail the service, so that SCM will
bring it up as per the settings in the recovery tab of the service
properties.

Environment.Exit(-1); will indicate to the SCM that the service
failed and it will restart the service.

Scenario 2 : During startup of the service, a critical resource is

not available and have to stop the service.

================================================== ===========================

private System.Timers.Timer timerStopCurrentService = null;
private MyService()
{
....
timerStopCurrentService = new System.Timers.Timer(1000);
timerStopCurrentService.Enabled = false;
timerStopCurrentService.Elapsed += new
System.Timers.ElapsedEventHandler(timerStopCurrent Service_Elapsed);
....
}

private void timerStopCurrentService_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
StopCurrentService();
}

public void ShutdownService()
{
ServiceController ourService = new ServiceController("ServiceName");
if( ourService.Status == ServiceControllerStatus.Running)
{
StopCurrentService();
}
else
{
timerStopCurrentService.Start();
}
}

private void StopCurrentService()
{
ServiceController ourService = new ServiceController("ServiceName");
ourService.Stop();
}

Note : If you want to display a message box to the user use the foll.
MessageBox.Show("Error Mesage", "Application Name",
MessageBoxButtons.OK, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly);
================================================== ===========================

Regards, Joy

sf*****@yahoo.com (Keith) wrote in message news:<d1**************************@posting.google. com>...
I'm in the same boat as the fellow who posted this message back in
August:

Title : Windows Service, How does one make a service "fail" properly?
Author : Ross Bennett
Group : microsoft.public.dotnet.languages.csharp
URL : http://groups.google.com/groups?q=ro...phx.gbl&rnum=1

I need to find a way to cause the service to stop when it encounters a
fatal error during processing. Using the ServiceController framework
class is not an option, as doing so makes the stop appear to be
"normal" when in fact it is not.

Any ideas? Any pointers to documentation or examples?

I've been beating my head against the monitor looking for a solution
to this and have found nothing. Hard to believe that there are only
two of us out here who need to make a Windows Service behave this way.

Thanks
SFN

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CG | last post by:
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...
2
by: Vince Keller | last post by:
I dont know if this is the correct newsgroup to post questions on Windows Service. I am trying to install and uninstall a Windows Service programmatically. As far as I know, there isnt a class...
0
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows...
9
by: Hardy Wang | last post by:
Hi all: I read an article from http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp about how to install a windows service programmatically. Based ont the code...
7
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web...
0
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then...
2
by: Ian | last post by:
I'm investigating a requirement where we've got a number of servers running the same web service using network load balancing to provide resilience. In the event that the web service encounters a...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
10
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.