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
>
>