Click to See Complete Forum and Search --> : Thread doesn't suspend!!!


spsj21
February 6th, 2009, 03:00 AM
Hi everybody!
I am trying to suspend and resume a thread using two 'Menu' buttons 'Start' & 'Stop'. I have created the thread as below

CWinThread *PWinThread;

OnCreate()
{
int Code = 1;
PWinThread = AfxBeginThread(ThreadFunction, &Code);
}

OnStart()
{
PWinThread->ResumeThread();
}

OnStop()
{
DWORD ExitCode;

PWinThread->SuspendThread();

::GetExitCodeThread(PWinThread->m_hThread, &ExitCode);
if(ExitCode == STILL_ACTIVE)
::MessageBox(0,"Thread","Is still Running",0);
}

When i click on 'Stop' button, I get the messagebox indicating that thread is still active.........can somebody trace it out please..............

Codeplug
February 6th, 2009, 07:58 AM
Suspended or not, it's still active until the thread returns from its thread function.

gg

spsj21
February 8th, 2009, 11:42 PM
But this nature of a working thread doesn't serve any purpose............actually, in my application a thread is continuously writing data to a .txt file and the main application is reading that data for plotting it on the monitor using a software timer.........now suppose if i want to suspend this whole process then i have to kill the timer for stopping the main application job (which is happening) and anyhow suspend the thread (this is creating problem).............then how can i solve it alternatively............

Codeplug
February 9th, 2009, 06:58 AM
You don't use SuspendThread() as means for synchronization (as the documentation states). Use a synchronization object instead - like a manual reset Event. The main loop of the thread would then uses a wait-function to proceed only if the Event is in a signaled state.
http://msdn.microsoft.com/en-us/library/ms686353(VS.85).aspx

gg