Windows Service clean up threads on stop

Posted on 2013-08-28
Medium Priority
Last Modified: 2013-08-30
WHat would be the way to properly clean up these threads in the Windows Service OnStop?

Dim phoneWorkerThread As New Threading.Thread(AddressOf startPhoneMonitoring)
phoneWorkerThread.Name = "Thread " + " | [" + dataRowString + "] " + System.Guid.NewGuid.ToString()
phoneWorkerThread.IsBackground = True

Open in new window

Question by:lrbrister
  • 2
  • 2
LVL 83

Expert Comment

ID: 39449470
If I remember correctly, you maintain a list of these threads right? You would need to abort all these threads before the service dies.

Author Comment

ID: 39449508
  So along these lines (using the dictionaty list)

            Dim t As Thread
            For Each t In Diagnostics.Process.GetCurrentProcess().Threads
            Next t
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 39452470
This is not using dictionary and may result in trying to abort the UI thread as well. You would need to change the loop to

For Each t in ThreadDictionary.Keys

or similar.

Author Closing Comment

ID: 39452486
Bingo!  Thanks

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question