Click to See Complete Forum and Search --> : Thread Exit Problem


vkg_16my
December 28th, 2004, 12:48 AM
hi,

i am developing a software which has three controls
1) Scan(Push BUtton)
2) Stop(Push Button)
3) List Control

when i click on scan button it starts a scan thread. Scan Thread further starts 20 more threads and displays the results i nthe list control.
when i click the stop button then all the threads must be deleted not suspended and the display of the results must stop.

So my problem is on clicking stop button all the threads are not deleted also when i click scan again no display appears
please help me
thanks

Arjay
December 28th, 2004, 12:54 AM
There is quite a few ways things can go wrong, so can you create a small sample app and zip it up so we can take a look at it?
Arjay

vkg_16my
December 28th, 2004, 01:15 AM
below is the code sample:-

void ClassName::OnBnClickedButtonScan()
{
m_MainThread = AfxBeginThread(ScanThread,(LPVOID) this);
}


UINT ClassName::ScanThread(LPVOID pParam)
{
ClassName *Th = (ClassName *)pParam ;
Th->Scan();
return 0;
}

void ClassName ::Scan()
{
For Loop
{
switch(condition)
{
case 0:
another thread starts here;
case 1:
another thread starts here;
......................................................
}

}
}


i use a global bool variable for each thread to stop it but i am unable to stop.
when stop button is clicked i change the value of bool variable. and then i use ExitThread function from inside the thread to stop it but nothin gworks

Arjay
December 28th, 2004, 03:28 AM
In your ClassName class, create an event that is manual reset. In the Scan() method, pass this event to each new thread. Within each thread, use WaitForSingleObject( hEvent, 0 ) == WAIT_OBJECT_0 to check for exit (like you are doing with the global variable). When you hit the Stop button, call SetEvent on the event.

Btw, in general it isn't a good idea to make so many threads in an app. Consider using QueueUserWorkItem() if running on NT4/Win2K or higher. This function creates a thread pool and manages it for you.

Arjay

vkg_16my
December 28th, 2004, 05:04 AM
where should i put the code WaitForSingleObject( hEvent, 0 ) == WAIT_OBJECT_0
whithin the threads. Also i have to delete the threads while the are executing
What is hEvent i am unable to understand
(Premature termination)

Andreas Masur
December 28th, 2004, 05:42 AM
Take a look at the following FAQ (http://www.codeguru.com/forum/showthread.php?t=305166)...

vkg_16my
December 28th, 2004, 06:26 AM
thanks andereas but i have to delete the threads in running state i am using ExitThread(0) within the thread's but it is not working.the display is not stopped.
i thing there is some problem of thread security access rights

please help me
thanks

Arjay
December 28th, 2004, 12:46 PM
thanks andereas but i have to delete the threads in running state i am using ExitThread(0) within the thread's but it is not working.the display is not stopped.
i thing there is some problem of thread security access rightsSend us a small zipped sample. I know you included some pseudo code earlier, but send us the actual code (well at least the thread proc).

Arjay

vkg_16my
December 29th, 2004, 08:18 AM
thanks Arjay

i have solved the problem by using your idea. Now i am able to use WaitForObject function.

thanks