473,569 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handle STOP in a multi-threaded Windows service

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
ThreadAbortedEx ception, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?

Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon
Jul 21 '05 #1
2 7205
Inline

Willy.

"Simon Niederberger" <si***@sincore. ch> wrote in message
news:Om******** *****@TK2MSFTNG P14.phx.gbl...
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

Such high number of threads is just a waste of CPU and memory resources, you
can do with far less than that.

I'm wondering what exactly happens when I stop the service. I don't see an
ThreadAbortedEx ception, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?
The OS doesn't send anything at all, the CLR stops the threads cold by
issuing a Thread.Abort as part of the CLR's orderly shutdown procedure. If
this is not what you want, you should handle your thread shutdown yourself
in OnStop. One way of doing this is to have your threads inspect a shared
flag that you could set in OnClose, once the flag set, the active thread
should stop it's activity and return, the OnStop procedure can use
Thread.Join(tim eout) on each thread you have started.
Note that all this should be done within 30 seconds, as otherwise the SCM
will consider that the service failed to shutdown.
Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon

Jul 21 '05 #2
Thanks a lot, perfect answer (within hours!) I'll look into reducing the
number of threads, but I feel that won't be to easy.

Cheers
Simon
"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:ui******** ******@TK2MSFTN GP09.phx.gbl...
Inline

Willy.

"Simon Niederberger" <si***@sincore. ch> wrote in message
news:Om******** *****@TK2MSFTNG P14.phx.gbl...
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

Such high number of threads is just a waste of CPU and memory resources,

you can do with far less than that.

I'm wondering what exactly happens when I stop the service. I don't see an ThreadAbortedEx ception, but I'm worried my worker threads might be
terminated in the middle of processing. My Server.OnStop is empty.

Does Windows wait for all threads to terminate in a given timespan, then
send an Abort to all running threads? Or does it send Abort directly?

The OS doesn't send anything at all, the CLR stops the threads cold by
issuing a Thread.Abort as part of the CLR's orderly shutdown procedure. If
this is not what you want, you should handle your thread shutdown yourself
in OnStop. One way of doing this is to have your threads inspect a shared
flag that you could set in OnClose, once the flag set, the active thread
should stop it's activity and return, the OnStop procedure can use
Thread.Join(tim eout) on each thread you have started.
Note that all this should be done within 30 seconds, as otherwise the SCM
will consider that the service failed to shutdown.
Any help, including pointing to good URL (haven't found any so far) or
better newsgroup is very much appreciated.

Simon


Jul 21 '05 #3

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

Similar topics

3
3287
by: B Moor | last post by:
SServer PC: Win SBS 2003 with 2.6 GHz processor and 1GB RAM SQL Server 2000 v 2000.8.00.76 (sp3) MS Office 2k3 MSJet ms04-014 (latest ost sp8) MDAC v2.8 RTM ADO 2.1 vb6.exe / ADO 2.0 I think this is a SQL Server/ADO problem as I have 2 applications with
6
2846
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in the first seconds the communication is ok, later i receive read/write error. I?ve been in MSDN site and there i discover that the read/write error...
12
3734
by: Aaron Smith | last post by:
What is the best way to handle data in a multiple user environment? We have forms that will allow users to add edit and delete data from a table on SQL server. The data could be edited on multiple machines at the same time. How do you keep the dataset constantly updated with changes made to the data? I'm playing with just calling the fill...
4
1513
by: Niels Jensen | last post by:
Hi, I'm a bit of a newbie to VB.NET so please forgive me if I get the terminology incorrect... I'm still learning :) I have a form with five numUpDown controls representing different ratios for 5 diferent things. Next to them are 5 checkboxes that can be used to lock it's relevant ratio. I'm trying to get them to automatically distribute...
4
3862
by: Jackson Peebles | last post by:
Hey everyone! I'm a complete newbie to PHP, and am trying to teach myself how to make some scripts. So far I've done pretty good, but even after searching through all my books, articles, manuals, and search engines on PHP, I can't figure out how to stop PHP from rounding. I am trying to create a calculator that calculates a diameter * pi,...
1
2378
by: rapin | last post by:
How do you make javascript’s string functions (like toUpperCase() or substr()) run under SpiderMonkey to handle multi-byte characters. For example: var strTest = "ørnen på"; var strWork = strTest.toUpperCase(); print( strWork ); The result is øRNEN På
0
2444
by: =?Utf-8?B?aGVyYmVydA==?= | last post by:
I read from a serialport using a worker thread. Because the worker thread t does not loop often, I cannot wait to terminate the worker thread using a boolean in the While condition. So I have a StopReader() method that simply aborts the worker thread (is there a better way for the above situation?). The StopReader creates an...
4
2588
by: Rui Maciel | last post by:
I want to support Unicode on a pet project of mine (small markup language parser). I've read a bit about Unicode (didn't delved beyond the basics) and I searched for some info on how to support Unicode on C programs. Unfortunately I wasn't able to find articles that could be considered more than loose ends, small blog entries and side remarks,...
8
2883
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2 etc. the child process is interactive: it asks for input then spits out some output, asks for more input then spits out some output. for example,...
4
2697
by: MartinRinehart | last post by:
Thinking about unclosed multi-line quotes. When you open a multi-line quote (type '"""') what does your editor do? Does it color the remainder of your text as a quote, or does it color the line with the open quote as a quote and leave the rest of your code alone? What do you want it to do? This is a tokenizer question in disguise, of...
0
7941
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. ...
0
8147
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...
0
6320
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...
1
5520
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...
0
5247
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3680
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...
0
3669
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1238
muto222
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.