473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Another Windows Service Question

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
8 1412
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric Chong | last post by:
I created a Windows Service in C# that requires to get passed command arguments like a Console App. I noticed that there is an option "Start parameters" text box in the property of a Windows...
4
by: Bill Sonia | last post by:
I'm written a Windows Service to send e-mails on events like OnStart, OnStop, OnShutDown using System.Web.Mail. It works for everything but OnShutdown. My guess is that once my send mail code is...
2
by: Lord Brett Sinclair | last post by:
Hello everybody I created a windows services with an msi file. I have another small vb.net exe that I would like to install when I install the windows service. I tried to create a merge module...
5
by: Richard Steele | last post by:
I have created a WinForm application that needs to be run as a windows service (the PC is inaccesible by any user) i have successfully installed the application as a windows service. When i start...
4
by: Ian | last post by:
Can anyone help I have a web server on the internet with and ASP.NET application on it, the application is set to allow Anonymous Access and Integrated Windows. The Web.config is set to use...
0
by: Charles Leonard | last post by:
I am having yet another issue with Windows Server 2003. This time, the web service (a file import web service) appears to run except for one odd message: "ActiveX component can't create object". ...
2
by: deko | last post by:
When to use a privileged user thread rather than a windows service? That's the question raised in a previous post . It was suggested that if the service needs to interact with a WinForms app...
5
by: Tom | last post by:
Using multiple System.Timers.Timer objects in a Windows Service for performing multi-thread activities in a periodic fashion. Timers are AutoReset=false, to only have a single timer execution...
2
by: =?Utf-8?B?Sm9obiBG?= | last post by:
Hello All, I have a question about using an ActiveX control with a Windows Servce in C#. I believe it is not possible to properly setup an ActiveX control in a windows service as the particular...
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
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,...
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...
1
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
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,...
1
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.