473,888 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Canceling Windows Service Start

I've written a Windows Service using C#, but I cannot seem to figure out how
to cancel or prevent the service from starting under certain conditions.
For example, in the OnStart I read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc) I don't want the servie to start. So how would I
cancel the start up?
Nov 17 '05 #1
12 2785
Anyone have an answer?

"JSheble" <js************ @logicor.com> wrote in message
news:ee******** ******@tk2msftn gp13.phx.gbl...
I've written a Windows Service using C#, but I cannot seem to figure out
how to cancel or prevent the service from starting under certain
conditions. For example, in the OnStart I read in some configuration
settings, and if anything is either empty, or invalid (such as a DB
DataSource string or a missing diredtory, etccc) I don't want the servie
to start. So how would I cancel the start up?

Nov 17 '05 #2
Hi,
I dont have a very good knowledge on this. But from what I know u cannot
start or stop a servce within a service. This has to be done by a writing a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc) The above portion has to be done by the Controller and then the service has
to be started.
PLease read more about monitoring windows services in MSDN

"JSheble" wrote:
I've written a Windows Service using C#, but I cannot seem to figure out how
to cancel or prevent the service from starting under certain conditions.
For example, in the OnStart I read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc) I don't want the servie to start. So how would I
cancel the start up?

Nov 17 '05 #3
I am not talking about a monitoring app, I'm talking I don't want the
service itself to start unless certain conditions are met. If they try to
start the service, and those conditions fail, then the servie should not
start. How does the service not allow itself to be started?

"Ravi" <Ra**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi,
I dont have a very good knowledge on this. But from what I know u cannot
start or stop a servce within a service. This has to be done by a writing
a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc)

The above portion has to be done by the Controller and then the service
has
to be started.
PLease read more about monitoring windows services in MSDN

"JSheble" wrote:
I've written a Windows Service using C#, but I cannot seem to figure out
how
to cancel or prevent the service from starting under certain conditions.
For example, in the OnStart I read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc) I don't want the servie to start. So how would
I
cancel the start up?

Nov 17 '05 #4
Throw an exception and your service will die.

Willy.

"JSheble" <js************ @logicor.com> wrote in message
news:ut******** ******@TK2MSFTN GP15.phx.gbl...
I am not talking about a monitoring app, I'm talking I don't want the
service itself to start unless certain conditions are met. If they try to
start the service, and those conditions fail, then the servie should not
start. How does the service not allow itself to be started?

"Ravi" <Ra**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi,
I dont have a very good knowledge on this. But from what I know u
cannot
start or stop a servce within a service. This has to be done by a writing
a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or a
missing diredtory, etccc)

The above portion has to be done by the Controller and then the service
has
to be started.
PLease read more about monitoring windows services in MSDN

"JSheble" wrote:
I've written a Windows Service using C#, but I cannot seem to figure out
how
to cancel or prevent the service from starting under certain conditions.
For example, in the OnStart I read in some configuration settings, and
if
anything is either empty, or invalid (such as a DB DataSource string or
a
missing diredtory, etccc) I don't want the servie to start. So how
would I
cancel the start up?


Nov 17 '05 #5
thank you!

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Throw an exception and your service will die.

Willy.

"JSheble" <js************ @logicor.com> wrote in message
news:ut******** ******@TK2MSFTN GP15.phx.gbl...
I am not talking about a monitoring app, I'm talking I don't want the
service itself to start unless certain conditions are met. If they try to
start the service, and those conditions fail, then the servie should not
start. How does the service not allow itself to be started?

"Ravi" <Ra**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi,
I dont have a very good knowledge on this. But from what I know u
cannot
start or stop a servce within a service. This has to be done by a
writing a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or
a
missing diredtory, etccc)
The above portion has to be done by the Controller and then the service
has
to be started.
PLease read more about monitoring windows services in MSDN

"JSheble" wrote:

I've written a Windows Service using C#, but I cannot seem to figure
out how
to cancel or prevent the service from starting under certain
conditions.
For example, in the OnStart I read in some configuration settings, and
if
anything is either empty, or invalid (such as a DB DataSource string or
a
missing diredtory, etccc) I don't want the servie to start. So how
would I
cancel the start up?



Nov 17 '05 #6
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing an
exception? Is there a certain type of exception I should throw?
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Throw an exception and your service will die.

Willy.

"JSheble" <js************ @logicor.com> wrote in message
news:ut******** ******@TK2MSFTN GP15.phx.gbl...
I am not talking about a monitoring app, I'm talking I don't want the
service itself to start unless certain conditions are met. If they try to
start the service, and those conditions fail, then the servie should not
start. How does the service not allow itself to be started?

"Ravi" <Ra**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
Hi,
I dont have a very good knowledge on this. But from what I know u
cannot
start or stop a servce within a service. This has to be done by a
writing a
Service Controller.
"read in some configuration settings, and if
anything is either empty, or invalid (such as a DB DataSource string or
a
missing diredtory, etccc)
The above portion has to be done by the Controller and then the service
has
to be started.
PLease read more about monitoring windows services in MSDN

"JSheble" wrote:

I've written a Windows Service using C#, but I cannot seem to figure
out how
to cancel or prevent the service from starting under certain
conditions.
For example, in the OnStart I read in some configuration settings, and
if
anything is either empty, or invalid (such as a DB DataSource string or
a
missing diredtory, etccc) I don't want the servie to start. So how
would I
cancel the start up?



Nov 17 '05 #7

"JSheble" <js************ @logicor.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing an
exception? Is there a certain type of exception I should throw?


protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Serv ice failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Applicatio n" eventlog.

Willy.
Nov 17 '05 #8
That's exactly what I have in the code, but the service still shows as
running in the SMC...

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:u0******** ******@TK2MSFTN GP10.phx.gbl...

"JSheble" <js************ @logicor.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing
an exception? Is there a certain type of exception I should throw?


protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Serv ice failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Applicatio n" eventlog.

Willy.

Nov 17 '05 #9
Should work for you as well, try to remove all code there is in OnStart and
leave the throw clause. If this doesn't work either post your code
(OnStart).

Willy.

"JSheble" <js************ @logicor.com> wrote in message
news:Og******** ******@tk2msftn gp13.phx.gbl...
That's exactly what I have in the code, but the service still shows as
running in the SMC...

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:u0******** ******@TK2MSFTN GP10.phx.gbl...

"JSheble" <js************ @logicor.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. .
Is there anything special I should know about this? I tried throwing an
exception in my OnStart, but the service still shows as running in the
SMC... Is there something else that has to occur besides just throwing
an exception? Is there a certain type of exception I should throw?


protected override void OnStart(string[] args)
{
...
// throw whatever Exception
throw (new Exception("Serv ice failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Applicatio n" eventlog.

Willy.


Nov 17 '05 #10

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

Similar topics

7
8673
by: Mullin Yu | last post by:
if i put the same code at the windows application or console, i can logon to the computer. but, if i put the same code at the windows service and start it, i still can't logon to the machine. the return value is TRUE. i tested it by going to windows explorer and then click that computer. my coding at windows service:
2
21926
by: Mark | last post by:
I created an extremely simple windows service that only writes to the EventLogs on Stop and Pause. I installed it using the InstallUtil.exe program, the output of which is below. It appears to be successful. I'm now ready to start my service (I think) but the NET START command does not appear to indicate that the service is available to be run. The output for NET START is below as well. The Services list in the W2K administrator...
1
3457
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. I'm having trouble getting the code that I've written to work, can anyone shed some light as to where I'm going wrong. The program has been written to do the following tasks: - Select remote server - Select from two specific services
0
3947
by: Scott Davies | last post by:
Hi, I'm looking for some help on a small program that I'm trying to develop in VB.NET. The program I'm trying to develop needs to be able to do the following: - Select remote server - Select from two specific services - Check the status of the server
3
3694
by: Doug Bailey | last post by:
I am trying to control a Windows Service via a Web Service interface. (I am developing in .NET2003) I am using the ServiceController object which allows me to read the state of the services with no problems. However, I am not able to start or stop the service unless I go through the process of impersonating an administrative user. (See MSDN KB 306158) Since it appears to be a privilege issue, I set the folders in IIS holding the Web...
7
2423
by: Ahmed Perlom | last post by:
Hi all, I am trying to start a windows application that has a GUI from a Windows service written in .NET 2.0. I have been searching on this for few days now with no avail. When using the System.Diagnostic.Process object to start the application (i.e Notepad), the new app runs and it is listed on the task manger list, but the GUI doesn't show up in the desktop of the current user. I am aware windows service (either LocalSystem,...
4
4196
by: tshad | last post by:
What would be a good way to check programmatically whether a service was running? We have a service that dies periodically and I need to check to see if this service is running. I know how to check to see if the status is in stopped or running mode. But that doesn't tell me if it is actually running. I need to know this so that if it happens I can programmatically start the same service on another machine.
1
15122
by: TJ | last post by:
I created a windows service using VS2005. I created/adjusted the install class using the designer controls. I set the user to be 'local system'. I installed the service using the VS2005 cmd console (installutil.exe). The service shows up fine in my services snap-in. I can start and stop it just fine and it works great when I start it. I can open a command window and type: net start "My Service" and net stop "My Service" and it works...
5
3319
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name? Why do I need to set a property within my code to the service name? Are all these required or am I just doing this for consistency purposes?
0
11182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10886
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10439
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9597
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4642
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4245
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.