472,780 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 1381
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.