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 / Visual Studio.NET / General / April 2006

Tip: Looking for answers? Try searching our database.

Help on Thread Terminating

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chaw-Chi Yu - 30 Apr 2006 05:00 GMT
Hi,

I need help in terminating threads in my application. My application is
written in C# and uses ThreadPoll to launch threads. Following is the
sample:

ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProcA));
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProcB));
..

void ThreadProcA(object state)
{
...
}

void ThreadProcB(object state)
{
...
}

The two threads are in a forever loop doing something. How do I kill
these two threads externally from the process that launches them? The
threads themselves have logic inside to exit when something happen but I
also want to be able to terminate the threads outside the threads.

Please help.

Thanks,
Chaw-Chi Yu
Barry Kelly - 30 Apr 2006 17:12 GMT
> The two threads are in a forever loop doing something. How do I kill
> these two threads externally from the process that launches them? The
> threads themselves have logic inside to exit when something happen but I
> also want to be able to terminate the threads outside the threads.

There are two things to be aware of: (1) terminating a thread (any
thread) is an unsafe operation, and isn't something you want to be doing
unless you are bringing down your application, and (2) you can't
terminate a ThreadPool thread directly. If you *must*, then create your
own threads that you can terminate.

The best approach when your threads are constantly busy (usually this
means in a loop) is to have some boolean field that your methods can
see, and for them to poll this variable and quit when the value changes.

-- Barry

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.