PC Review


Reply
Thread Tools Rate Thread

Wait for Thread normally terminate

 
 
=?Utf-8?B?amVmZg==?=
Guest
Posts: n/a
 
      8th Mar 2007
Hi Guys

There is a couple of threads run on my application.
before I quit the main process, i want to stop all the thread,
and then quit main process.

I have set some flag to stop my thread,
something like

void run()
{
while(!stop){
.....
}
}

but what I want to double-confirm is :

void quit(){
waitfortermation(firstThread);//kind of
waitfortermation(secodThread);//kind of
....
}
Can I implement some function like this?
 
Reply With Quote
 
 
 
 
Paul G. Tobey [eMVP]
Guest
Posts: n/a
 
      8th Mar 2007
WaitForSingleObject(), the Win32 API call, can be used to wait on a thread
handle (you'll have to get the handle, of course). For multiple threads,
you could also P/Invoke WaitForMultipleObjects().

thread.Join() is the usual way to wait for thread to exit...

Paul T.

"jeff" <(E-Mail Removed)> wrote in message
news:E28EC662-5F3B-462B-8830-(E-Mail Removed)...
> Hi Guys
>
> There is a couple of threads run on my application.
> before I quit the main process, i want to stop all the thread,
> and then quit main process.
>
> I have set some flag to stop my thread,
> something like
>
> void run()
> {
> while(!stop){
> .....
> }
> }
>
> but what I want to double-confirm is :
>
> void quit(){
> waitfortermation(firstThread);//kind of
> waitfortermation(secodThread);//kind of
> ....
> }
> Can I implement some function like this?



 
Reply With Quote
 
Norman Rericha
Guest
Posts: n/a
 
      8th Mar 2007
When I create threads, I try to build in an external termination
system. Usually this consists of a while loop that checks a variable
local to the class. I then also have a variable local to the class
that says if the thread is running.

Example

this.ThreadARunning = true;
while(this.AllowThreadAToRun)
{
//Do stuff here

}
this.ThreadARunning = false;

Now when I want to abort the thread, or cause it to stop executing, I
design the "main" thread function to loop. Stopping mid-execution could
cause alot of data headaches. From outside the thread I can watch the
"ThreadAIsRunning" variable to determine if the thread died.
So using your example code, my code would work like this.

this.AllowThreadAToRun = false;
this.AllowThreadBToRun = false;

while(this.ThreadARunning)
{
Application.DoEvents();
}
while(this.ThreadBRunning)
{
Application.DoEvents();
}

Of course you can expand this as you see fit for error checking,
securing data during the abort, and resetting the system to a known
state. But that is upto you.

I like this method because I can guarantee a clean exiting of the
thread and I know for a fact that any clean up the thread needed to do
has been done.

Hope this helps

--
Norman Rericha
Sr. Software Architect
Embedded Systems
4Techwork.com


 
Reply With Quote
 
maxxel
Guest
Posts: n/a
 
      8th Mar 2007
jeff je wrote:
> There is a couple of threads run on my application.
> before I quit the main process, i want to stop all the thread,
> and then quit main process.


In CF1 I use ManualResetEvent initially set to false,
and run method like this (without cleanup):

void run() {
while (!stop){
...
}
event.Set();
}

and StopThread would be like:

void StopThread() {
stop = true;
event.WaitOne();
}

In CF2 there is no need for Event because you can join a thread:

void StopThread() {
stop = true;
m_Thread.Join();
}

hope this helps,
maxxel

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
for some reason webbrowser1.Navigate() works but causes thread to terminate, i want to do a GetElementyByID in the same thread that executes the navigate.. i am totaly stumped: http://rafb.net/p/LO2X6X82.html DR Microsoft C# .NET 2 15th Nov 2007 12:15 PM
for some reason webbrowser1.Navigate() works but causes thread to terminate, i want to do a GetElementyByID in the same thread that executes the navigate.. i am totaly stumped: http://rafb.net/p/LO2X6X82.html DR Microsoft Dot NET Framework Forms 0 15th Nov 2007 09:17 AM
for some reason webbrowser1.Navigate() works but causes thread to terminate, i want to do a GetElementyByID in the same thread that executes the navigate.. i am totaly stumped: http://rafb.net/p/LO2X6X82.html DR Microsoft Dot NET Framework 0 15th Nov 2007 09:16 AM
for some reason webbrowser1.Navigate() works but causes thread to terminate, i want to do a GetElementyByID in the same thread that executes the navigate.. i am totaly stumped: http://rafb.net/p/LO2X6X82.html DR Microsoft Dot NET 0 15th Nov 2007 09:16 AM
How can you wait for the SHELL command to terminate ? jmoffat Microsoft Excel Programming 2 1st Oct 2003 10:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 AM.