Click to See Complete Forum and Search --> : can anybody pleeeeeeeeeease help me......


spsj21
February 26th, 2009, 11:50 PM
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..............

spsj21
February 26th, 2009, 11:52 PM
Please dont skip if u know the answer.........

Codeplug
February 26th, 2009, 11:57 PM
See post #2 from: http://www.codeguru.com/forum/showthread.php?t=470282

gg

Arjay
March 1st, 2009, 06:07 PM
Looks like CodePlug already answered in the other post.

You're getting the terminology between pausing and stopping messed up.

Stopping generally means to end the thread operation. Pausing means to suspend the operation of the thread. In other words, it means to temporarily halt it, so it can be resumed later. A stopped thread cannot be resumed - it can only be restarted it.

So now on to your code. When you suspend the thread and then use GetExitCodeThread to check the thread status, you get a STILL_ACTIVE because the thread is still active (it might be in a suspended state, but this is considered active because the thread hasn't exited or been terminated).

shaan.mastermind
March 15th, 2009, 12:14 PM
Hi,

Well, you want to stop the thread whereas you are attempting to suspend it. Suspension of a thread will keep it still active (only suspend its execution till you resume it again).

In your case you may want to conditionally let the thread complete it execution in the OnStop handler. This can be achieved using a condition processing logic within your thread routine.

Hope it serves your need

love-n-peace,
~shaan
http://objects.blog.co.in/