Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2006

Tip: Looking for answers? Try searching our database.

Windows Service and Threading

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Techno_Dex - 13 Jul 2006 14:57 GMT
What is the proper way to terminate a Thread in a Windows Service?  I have a
windows service which starts up three threads which run in a while loop as
long as a boolean flag is set to true.  These threads each sleep for X
ammount of time then start processing again.  Since this is a service it's
basically running in an infinite loop.  When the user goes to stop the
service, I have been calling the Thread.Abort() method, which throws a
ThreadAbortedException.  I've also tried using the Thread.Interupt() method
to wake any sleeping threads, but that too throws a
ThreadInteruptedException.  What is the proper way to kill these threads in
the 30 second timeframe that the windows service framework gives a service
to respond back?  If catching these Exceptions are the correct way to stop
the service, what is the correct way to know if the service actually dies on
it's own for some reaon (i.e. god forbid locking)?
Kevin Spencer - 13 Jul 2006 18:47 GMT
Why not use a timer and run the threads when you need to, rather than
putting them to sleep? That way, all you have to do is stop the timer.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.

> What is the proper way to terminate a Thread in a Windows Service?  I have
> a windows service which starts up three threads which run in a while loop
[quoted text clipped - 9 lines]
> to stop the service, what is the correct way to know if the service
> actually dies on it's own for some reaon (i.e. god forbid locking)?
Techno_Dex - 13 Jul 2006 20:57 GMT
Why waste the overhead time of initializing a Thread and tearing it down
after every execution?  More time and resources would be wasted getting
everything configured and up and running for the Thread each time.  So back
to my original question?

> Why not use a timer and run the threads when you need to, rather than
> putting them to sleep? That way, all you have to do is stop the timer.
[quoted text clipped - 13 lines]
>> service actually dies on it's own for some reaon (i.e. god forbid
>> locking)?
William Stacey [MVP] - 14 Jul 2006 01:59 GMT
Running is pooling loop is not right either.  A ~better way is to have
thread block on an empty work queue.  A clean way to stop is to enqueue
multiple "Close" objects to the queue, so each thread will see a
Close/Shutdown object and exit nicely.  Another way is to flip your flag (in
a sync context - lock) then Interrupt each thread.  Each thread will get the
Interrupt exception which they will catch and drop out of their loops
respectively.  Don't use Abort.

Signature

William Stacey [MVP]

| Why waste the overhead time of initializing a Thread and tearing it down
| after every execution?  More time and resources would be wasted getting
[quoted text clipped - 18 lines]
| >> service actually dies on it's own for some reaon (i.e. god forbid
| >> locking)?
William Stacey [MVP] - 14 Jul 2006 02:04 GMT
should be "Running in a pooling loop is not right either".

Signature

William Stacey [MVP]

| Running is pooling loop is not right either.  A ~better way is to have
...
Kevin Spencer - 14 Jul 2006 11:24 GMT
You have a point. Actually, thinking back on my own solution, what I did was
to put the timer into the thread. The thread creates an instance of a class
with a timer in it, which fires and triggers an action. The class in the
thread can then be signalled to stop the timer.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.

> Why waste the overhead time of initializing a Thread and tearing it down
> after every execution?  More time and resources would be wasted getting
[quoted text clipped - 18 lines]
>>> if the service actually dies on it's own for some reaon (i.e. god forbid
>>> locking)?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.