473,748 Members | 4,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to stop a Windows Service from within itself?

Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #1
6 28611
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #2
:)

--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

"John Timney (ASP.NET MVP)" <ti*****@despam med.com> wrote in message
news:ed******** *****@TK2MSFTNG P15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst


Nov 17 '05 #3
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despam med.com> wrote in message
news:ed******** *****@TK2MSFTNG P15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst


Nov 17 '05 #4
It does work from the OnStart method...
"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:eP******** ******@TK2MSFTN GP10.phx.gbl...
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despam med.com> wrote in message
news:ed******** *****@TK2MSFTNG P15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst



Nov 17 '05 #5
Where are you trying to stop the service from? I don't think that you can
stop it from the initialization or start method in the way that you are
describing.

--
Jared Parsons [MSFT]
ja******@online .microsoft.com
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst

Nov 17 '05 #6
Have you tried a simple Application.Exi t()

You might have to call exitthread before calling exit.

The service itself must have the CanStop property set to true when your
developing the service. You can check if you can stop it with .......
if (myController.C anStop)
{
myController.St op();
}
The suggestion to throw an exception is a complete hack that another MVP
suggested worked. The reason it works in onStart is that the service has
not yet started.

Your alternative would be to overide the DACL for the service, allocating a
new security descriptor and then using a call to advapi32 to get a better
control at the API level. It still might not work, as the user account your
service runs under has to have service stop permissions so check which
account yout running the service under.

-- Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
It does work from the OnStart method...
"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:eP******** ******@TK2MSFTN GP10.phx.gbl...
I tried it, but no cigar.

I am starting a timer in the OnStart method

and the worker code is started from the timer event.

Wherever I throw an unhandled exception, the service is not stopped...

"John Timney (ASP.NET MVP)" <ti*****@despam med.com> wrote in message
news:ed******** *****@TK2MSFTNG P15.phx.gbl...
Throw an unhandled exception and your service should stop.

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

"Jacobus Terhorst" <ja*****@usa.ne t> wrote in message
news:Os******** *****@TK2MSFTNG P15.phx.gbl...
Using C#:

I tried:
ServiceControll er me = new ServiceControll er(this.Service Name);
me.Stop();

it raises an exception: Cannot find Service
I also tried:

Process.Start(" net.exe", "stop " + this.ServiceNam e);
That did work either.
from DOS:
net stop ServiveName
works.

Any suggestions welcome.

Jacobus Terhorst




Nov 17 '05 #7

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

Similar topics

2
7212
by: Simon Niederberger | last post by:
Hi I've written a Windows Service which has - several (0-100) listeners threads which spawn worker threads based on events, timers etc - several (0-300) worker threads which handle data (message based) and terminate after processing I'm wondering what exactly happens when I stop the service. I don't see an ThreadAbortedException, but I'm worried my worker threads might be
5
27342
by: Paul O. Morris | last post by:
Is there a script that I can run to stop a particular SQL server service on Win2003 server? I'm looking for a similar script to restart that service as well. Thanks.
1
2518
by: Mike001 | last post by:
Hi I have developed a Windows service in C# (inherited from System.ServiceProcess.ServiceBase) and, after the service has successfully started, when I catch an exception I'd like to write the code for the service to stop itself. Right now, when an exception occurs the service seems to halt but it still shows as "Started" in the Services window. Could someone please suggest the best way to stop the service so the "Started" doesn't appear ...
13
2593
by: Åženol Akbulak | last post by:
Hi, I have an asp.net application. And I have worker threads which must run always (7x24). I start my threads in Application_Start, and I stop its in Application_Stop events. I log this events. I see that when all sessions time out, Application stops. How can prevent application stop? -- ______________________________
2
7865
by: Nathan | last post by:
I have created a windows service but am having difficulties handling exceptions in the OnStart. What I want to do is if an exeception occurs during any of the code executed during the OnStart is to log the problem and stop the service. I've already sorted out the logging of the problem to the event log but the suggested solution I have implemented for stopping the service does not work. Currently I declare a ServiceController and set...
4
5606
by: Blaxer | last post by:
I have read approximately 30 articles now on various methods to make your vb.net application automatically update itself and I can't see how they apply to a vb.net windows services projects. The goal of the project is to write a windows service in vb.net that runs on many workstations that can automatically update itself from a central web server or share (either or doesn’t matter). I do not want the service to re-install itself each time...
7
7230
by: shai | last post by:
I am working at .net 1.1, writing in c#. I have windows service with a COM object. Every unexpected time The COM object throw an error that make my service get stuck (do not respond). I can catch this error. I want to restart my windows service every time the COM object throws an error. I use System.ServiceProcess.ServiceController to stop and start my service. But there is one thing I do not understand:
2
3249
by: Cappamontana | last post by:
Hi, We have a .Net project containing 1 Windows Application and 3 Windows services. The project is quite complex and is still in development. The services can be started and stopped by using the application. Also there are some timers in it which triggers the service on a specific time to start (scheduled). When the service done it stops itself so the resources come free.Though it is running fine since 4-5 months, now and than we get an error...
8
9388
by: Lemune | last post by:
Hi, I'm developing window service application on C# 2005. The service is to read a new excel file on certain directory, and process it to database. The service work find on XP. But when I install the application on Windows Server 2003, when i start the service it said: "The <my serviceon Local Computer started and then stop. Some service stop automatically if they have no work to do , for example ,
0
9544
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4606
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.