By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,953 Members | 1,547 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,953 IT Pros & Developers. It's quick & easy.

Another Windows Service Question

P: n/a
I want my service to terminate automatically if the specified parameters are
invalid. I tried to use a servicecontroller component to attach to the
service, but I think that it's failing because it's being called in the
OnStart event (where I validate the parameters) and the service isn't fully
running yet. How can I stop my service from within the service itself?
Thanks.

Jerry
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
why not let your service start cleanly, and then terminate it in your first
thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
I want my service to terminate automatically if the specified parameters are invalid. I tried to use a servicecontroller component to attach to the
service, but I think that it's failing because it's being called in the
OnStart event (where I validate the parameters) and the service isn't fully running yet. How can I stop my service from within the service itself?
Thanks.

Jerry

Nov 18 '05 #2

P: n/a
I replied to this yesterday, but apparently the post never made it...

Thanks for the response, John. I've tried just what you're suggesting.
Even if I do it after the OnStart event, what's the proper way to terminate
the service from within itself? Do you still have to use a
ServiceController object? In the timer's elapsed event, I tried somethign
like this, but it doesn't seem to do anything:

Sub...Elapsed Event...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.stop
Else
....
End If
End Sub

Is there a more appropriate way to stop the service? How soon after the
OnStart event finishes can you do this? Thanks.

Jerry


"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
why not let your service start cleanly, and then terminate it in your first thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
I want my service to terminate automatically if the specified parameters

are
invalid. I tried to use a servicecontroller component to attach to the
service, but I think that it's failing because it's being called in the
OnStart event (where I validate the parameters) and the service isn't

fully
running yet. How can I stop my service from within the service itself?
Thanks.

Jerry


Nov 18 '05 #3

P: n/a
Hi Jerry,

Well the approach I would suggest is unfortunately what your doing - you do
need a serviceController instance to stop it via code. The code I would use
is identical to your VB code (albeit c#).

ServiceController serviceMonitor = new ServiceController("servicename");
if( serviceMonitor == ServiceControllerStatus.Running )
{
serviceMonitor.Stop();
}

The only difference I can see would be that I would typically perform a
check to see if the service was actually running before trying to termnate
in case it had not actually started. You cant stop a service thats not
completed its onStart event, timer elapsed should not kick in until until
onStart completes.

Are you trying to stop the service, or actually terminate it, putting a
service into a stopped state is "usually" very straightforward and the code
you have should suffice - you cant easily however terminate a service
without being a bit of a hacker and actually starting it with process start
and killing the process ID.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

"Jerry Camel" <rl*****@msn.com> wrote in message
news:uQ*************@TK2MSFTNGP11.phx.gbl...
I replied to this yesterday, but apparently the post never made it...

Thanks for the response, John. I've tried just what you're suggesting.
Even if I do it after the OnStart event, what's the proper way to terminate the service from within itself? Do you still have to use a
ServiceController object? In the timer's elapsed event, I tried somethign
like this, but it doesn't seem to do anything:

Sub...Elapsed Event...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.stop
Else
...
End If
End Sub

Is there a more appropriate way to stop the service? How soon after the
OnStart event finishes can you do this? Thanks.

Jerry


"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
why not let your service start cleanly, and then terminate it in your

first
thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
I want my service to terminate automatically if the specified parameters
are
invalid. I tried to use a servicecontroller component to attach to

the service, but I think that it's failing because it's being called in the OnStart event (where I validate the parameters) and the service isn't

fully
running yet. How can I stop my service from within the service itself? Thanks.

Jerry



Nov 18 '05 #4

P: n/a
That's a great idea, but how do you terminate the thread from within? I
tried setting a flag and terminating in the timer event, but the service
never stopped. What's the code for stopping the service? And how long
after the OnStart event do you have to wait before you can terminate
cleanly?

This is what I tried (from memory, so this might not be perfect syntax):

...Sub TimerElapseEvent...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.Stop
Else
...
End If
End Sub

Is there a more appropriate way to stop the service?

Thanks for your help.

Jerry
"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
why not let your service start cleanly, and then terminate it in your first thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
I want my service to terminate automatically if the specified parameters

are
invalid. I tried to use a servicecontroller component to attach to the
service, but I think that it's failing because it's being called in the
OnStart event (where I validate the parameters) and the service isn't

fully
running yet. How can I stop my service from within the service itself?
Thanks.

Jerry


Nov 18 '05 #5

P: n/a
I just want to stop the service. At tehm oment, I'm just using and End
command which does the trick albeit without grace.
I'll play with it a bit, but I'm pretty sure that I'm not getting to my
Elapsed event until after the service is officially started. (I'm getting
the auto-logged "Started" event and timer.start is the last line of the
OnStart handler.) Besides, I even tried attaching to another running
service - one that'd been running since system start and I get the same
exception. Problem is that the exception doesn't say why it can't attach,
just that it can't...

But! As I'm typing this I just had an epif - ipiph -eepiph... I had an
idea! My test box is a Win2K3 server... And I was usign the new
NetworkService account... I'll be that it's not allowed to attach to a
service. What do you think? Well, something else to test, anyway...

Is there a way to specify that an account has the right to start and stop
specific services? Thanks for your insight...

Jerry

"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:ef**************@TK2MSFTNGP11.phx.gbl...
Hi Jerry,

Well the approach I would suggest is unfortunately what your doing - you do need a serviceController instance to stop it via code. The code I would use is identical to your VB code (albeit c#).

ServiceController serviceMonitor = new ServiceController("servicename");
if( serviceMonitor == ServiceControllerStatus.Running )
{
serviceMonitor.Stop();
}

The only difference I can see would be that I would typically perform a
check to see if the service was actually running before trying to termnate
in case it had not actually started. You cant stop a service thats not
completed its onStart event, timer elapsed should not kick in until until
onStart completes.

Are you trying to stop the service, or actually terminate it, putting a
service into a stopped state is "usually" very straightforward and the code you have should suffice - you cant easily however terminate a service
without being a bit of a hacker and actually starting it with process start and killing the process ID.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

"Jerry Camel" <rl*****@msn.com> wrote in message
news:uQ*************@TK2MSFTNGP11.phx.gbl...
I replied to this yesterday, but apparently the post never made it...

Thanks for the response, John. I've tried just what you're suggesting.
Even if I do it after the OnStart event, what's the proper way to

terminate
the service from within itself? Do you still have to use a
ServiceController object? In the timer's elapsed event, I tried somethign
like this, but it doesn't seem to do anything:

Sub...Elapsed Event...
If invalidParameters Then
Dim sc As New ServiceController(Me.ServiceName)
sc.stop
Else
...
End If
End Sub

Is there a more appropriate way to stop the service? How soon after the
OnStart event finishes can you do this? Thanks.

Jerry


"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:O7*************@tk2msftngp13.phx.gbl...
why not let your service start cleanly, and then terminate it in your

first
thread timer action should the params be missing or invalid. Services
follow a chain of events and you need to work within the chain.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:uy**************@TK2MSFTNGP10.phx.gbl...
> I want my service to terminate automatically if the specified

parameters are
> invalid. I tried to use a servicecontroller component to attach to the > service, but I think that it's failing because it's being called in the > OnStart event (where I validate the parameters) and the service isn't fully
> running yet. How can I stop my service from within the service itself? > Thanks.
>
> Jerry
>
>



Nov 18 '05 #6

P: n/a
Is there a way to specify that an account has the right to start and stop
specific services? Thanks for your insight...

Local security policy under user rights allows you to specify who can log on
as a service..............Why dont you just set your service to a really
high privelaged user and see if your error goes away.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP


Nov 18 '05 #7

P: n/a
For testing that would work, but I'm not sure the high mucky-mucks would
want a priveledged account assinged to a specific service. If you could
limit the priveledge to the specific service, that would be ideal. I'll
play with it. My production server isn't going to be a Win2K3 box, so I'm
not going to be able to use that service account anyway.

"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:uj**************@TK2MSFTNGP11.phx.gbl...
Is there a way to specify that an account has the right to start and stop specific services? Thanks for your insight...

Local security policy under user rights allows you to specify who can log

on as a service..............Why dont you just set your service to a really
high privelaged user and see if your error goes away.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP

Nov 18 '05 #8

P: n/a
Then create an account that can log on as a service and use that named
account only for this particular service. It probably doesn't need to have
much in the way or authoritive rights - ie it doesn't need to be an admin,
just the right to operate as as service.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
"Jerry Camel" <rl*****@msn.com> wrote in message
news:#T**************@TK2MSFTNGP09.phx.gbl...
For testing that would work, but I'm not sure the high mucky-mucks would
want a priveledged account assinged to a specific service. If you could
limit the priveledge to the specific service, that would be ideal. I'll
play with it. My production server isn't going to be a Win2K3 box, so I'm
not going to be able to use that service account anyway.

"John Timney (Microsoft MVP)" <ti*****@despammed.com> wrote in message
news:uj**************@TK2MSFTNGP11.phx.gbl...
Is there a way to specify that an account has the right to start and stop specific services? Thanks for your insight...

Local security policy under user rights allows you to specify who can

log on
as a service..............Why dont you just set your service to a really
high privelaged user and see if your error goes away.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP


Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.