Click to See Complete Forum and Search --> : MessageQueue blocks Threads????


PerryRhodan
June 25th, 2009, 09:48 AM
Hi,

i run in a odd situation. I created a thread. This thread is running as long as a Event which i call KillSignal is set. This thread is a simple worker thread (no message queue). In a MFC Dialog a Methode is called when a button is pressed. In this method i want to stop the thread if running. So i set the KillEvent to Signaled and wait some time for the thread to stop working. But it won't stop. I have the impression that the thread is blocked while I'm in that methode which was called after the ButtonClick. As if i open a MessageBox, the thread seams to continue running and process the KillEvent....

see my code below, hope someone has a solution 4me.

Thx.

void Dialog::OnBnClickedBtnStop()
{
if(m_Thr.IsAlive())
m_Thr.TryKill(2000);

//=> the thread is here still alive no matter how long i wait.
if(m_Thr.IsAlive())
AfxMessageBox("Still alive!");

//=> Here, the thread got killed.....

}

HANDLE Thread::CreateThread(BOOL bSuspended,LPDWORD lpThreadId)
{
if(IsAlive())
{ASSERT(0);return NULL;}

return (HANDLE)::CreateThread(NULL,0,RunThread,this,bSuspended?CREATE_SUSPENDED:0,lpThreadId);
}

BOOL Thread::IsAlive(void) const
{ return m_hThread && (WAIT_OBJECT_0!=WaitForSingleObject(m_hThread,0));}

BOOL Thread::SignalKill() const
{
if(!IsAlive())
return TRUE;
if(!m_hKillEvent)
{ASSERT(0);return FALSE;}

return SetEvent(m_hKillEvent);
}

BOOL Thread::Wait4Exit(DWORD nMilliseconds) const
{
if(WAIT_OBJECT_0!=WaitForSingleObjectEx(m_hThread,nMilliseconds,false))
return FALSE;
return TRUE;
}

BOOL Thread::TryKill(DWORD nMilliseconds)
{
if(!IsAlive())
return TRUE;
if(!m_hKillEvent)
{ASSERT(0);return FALSE;}

if(!SignalKill())
return FALSE;

if(!Wait4Exit(nMilliseconds))
return FALSE;

return TRUE;
}

BOOL Thread::IsKillSignaled() const
{
return WAIT_OBJECT_0==WaitForSingleObject(m_hKillEvent,0);
}

unsigned Thread::Run()
{
while(!IsKillSignaled())
{
....
}
return 1;
}

PerryRhodan
June 25th, 2009, 10:42 AM
.... sry, just forget it. it's solved... ;)