473,418 Members | 2,052 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,418 software developers and data experts.

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 2743
Anyone have an answer?

"JSheble" <js************@logicor.com> wrote in message
news:ee**************@tk2msftngp13.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**@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.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**************@TK2MSFTNGP15.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**@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.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****************@TK2MSFTNGP15.phx.gbl...
Throw an exception and your service will die.

Willy.

"JSheble" <js************@logicor.com> wrote in message
news:ut**************@TK2MSFTNGP15.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**@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.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****************@TK2MSFTNGP15.phx.gbl...
Throw an exception and your service will die.

Willy.

"JSheble" <js************@logicor.com> wrote in message
news:ut**************@TK2MSFTNGP15.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**@discussions.microsoft.com> wrote in message
news:DD**********************************@microsof t.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***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" 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**************@TK2MSFTNGP10.phx.gbl...

"JSheble" <js************@logicor.com> wrote in message
news:%2***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" 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**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...

"JSheble" <js************@logicor.com> wrote in message
news:%2***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.


Nov 17 '05 #10
And make sure you refresh the service status in the SMC....

Willy.

"JSheble" <js************@logicor.com> wrote in message
news:Og**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...

"JSheble" <js************@logicor.com> wrote in message
news:%2***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.


Nov 17 '05 #11
Well I did exactly what you said, commenting out all the code in my OnStart,
threw an exception, looked in the SMC and it still shows as running... I can
even press the start and stop buttons in the SMC, and it stops and starts
with no problem...

Here's the OnStart code... as I said, it does nothing but throw the
exception:

protected override void OnStart(string[] args)
{
throw(new Exception("Service failed to start"));

// this.oAppSettings.ListenPort =
Convert.ToInt32(ConfigurationSettings.AppSettings["ListenPort"].ToString());
// this.oAppSettings.LogPath =
ConfigurationSettings.AppSettings["LogPath"].ToString();
// this.oAppSettings.TestPath =
ConfigurationSettings.AppSettings["TestPath"].ToString();
// this.oAppSettings.ConnectionTimeout =
Convert.ToInt32(ConfigurationSettings.AppSettings["ConnectionTimeout"].ToString());
// this.oAppSettings.DataDSN =
ConfigurationSettings.AppSettings["DataDSN"].ToString();
// this.oAppSettings.ShowGreeting =
Convert.ToBoolean(ConfigurationSettings.AppSetting s["ShowGreeting"].ToString());
// this.oAppSettings.GlobalParse =
Convert.ToBoolean(ConfigurationSettings.AppSetting s["GlobalParse"].ToString());
// this.oAppSettings.OnCustomParse =
ConfigurationSettings.AppSettings["OnCustomParse"].ToString();
// this.oAppSettings.OnResponse =
ConfigurationSettings.AppSettings["OnResponse"].ToString();
//
// if((this.oAppSettings.LogPath != "") &&
!this.oAppSettings.LogPath.EndsWith("\\"))
// {
// this.oAppSettings.LogPath += "\\";
// }
//
// if((this.oAppSettings.TestPath != "") &&
!this.oAppSettings.TestPath.EndsWith("\\"))
// {
// this.oAppSettings.TestPath += "\\";
// }
//
// if(this.oAppSettings.ConnectionTimeout > 0)
// {
// this.oAppSettings.ConnectionTimeout *= (1000 * 60);
// }
//
// // TODO add appropriate validation for config settings
// // TODO throwing an exception here should cause the OnStart to fail...
// // throw(new Exception("Service failed to start... config errors"));
//
// WMI_StatusProvider.FireEvent("GSRouter Service Started", "GSRouter",
EventType.ServiceStart);
// this.sockServer.Listen(this.oAppSettings.ListenPor t);
// this.dtUpTime = DateTime.Now;
}
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...

"JSheble" <js************@logicor.com> wrote in message
news:%2***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.



Nov 17 '05 #12
Please accept my most humblest aplogies... it turns out I had changed the
name of my servie exe file, but didn't change the batch file that installs
and starts the service... so the service that I was actually testing against
wasn't the service that had the exception code in it...

Again, my most humble apology...

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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**************@tk2msftngp13.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**************@TK2MSFTNGP10.phx.gbl...

"JSheble" <js************@logicor.com> wrote in message
news:%2***************@TK2MSFTNGP12.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("Service failed to init..."));
...
Make sure you don't catch the exception, the service should stop and
automatically write a stack trace to the "Application" eventlog.

Willy.



Nov 17 '05 #13

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

Similar topics

7
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. ...
2
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...
1
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...
0
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 -...
3
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...
7
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...
4
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...
1
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...
5
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?...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.