473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to stop windows service from within

I have a windows service that spawns several threads. One of those threads
may catch an exception that will require the whole service to shutdown. What
is the proper way to do it? I did some search on this subject, and the only
thing that I found is to create an instance of ServiceController and call
its Stop() method:

System.ServiceProcess.ServiceController sc = new
System.ServiceProcess.ServiceController("my Service Name");
sc.Stop();

This does not seem to work when I do it from a thread created by my service.
I'd like to avoid using System.Environment.Exit() since I am looking for a
solution that will cause OnStop() method of my service to be invoked prior
to termination of my service.

Any help would be greatly appreciated.

Vlad.
Nov 16 '05 #1
2 15991
Hi Vlad
From within your service threads , you can't call a stop on the your
service . if you want to do some finalization code before you exit , why
don't you explicitly all the Ontsop handling function ( that is attached by
a delegate to the event ) . so you explicitly call this.onstop() before
calling System.Environment.Exit() . or you can just replicate that code
in another routine.
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #2
> From within your service threads , you can't call a stop on the your
service.
My intuition failed me this time.
if you want to do some finalization code before you exit , why
don't you explicitly all the Ontsop handling function ( that is attached by a delegate to the event ) . so you explicitly call this.onstop() before
calling System.Environment.Exit(). or you can just replicate that code
in another routine.


Yep, that would work. Thanks

Vlad.
Nov 16 '05 #3

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

Similar topics

3
by: Jacob Crossley | last post by:
Hello all. We have about 10 Window's services that we wrote in c#. We use them to process row's that we have queued up in various SQL tables. The services seem to hang at least once in any given...
1
by: sidd | last post by:
Hi all, how do i exit out(unload) of a windows service in case of an error. so i have a code block as follows try { } catch(exception ex) { //log to event log
8
by: Jerry Camel | last post by:
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...
2
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...
3
by: Goran Djuranovic | last post by:
Hi all, I have a web app running on a local PC that can start and stop windows service on a remote machine, but only when I browse to it locally. If I browse to it from my other PC and try to...
1
by: =?Utf-8?B?cm9zczYxMw==?= | last post by:
I'm puzzled by an error message encountered while creating a Windows Service. In particular, I'm creating a second Windows Service within a Windows Service project, using Visual Studio 2005. The...
5
by: dm3281 | last post by:
I'm really starting to hate writing services -- or trying to, anyway. Why do I need to rename my project to the service name? Why do I need to set the "ServiceName" property to my service name?...
0
by: Srinivas M | last post by:
Hi, I have created a service that will monitor a folder. The problem here is , unable to stop the service even though am using service controller. can any one help me......... thanks in...
0
Pakmarshal
by: Pakmarshal | last post by:
Hi, I have developed a windows service which records the applications (processes) that runs on the machine. To get the process start trigger i have used wmi. Obj_ManagementStartWatcher = New...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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...

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.