Sorry guys for different topic on this list but I II start be
desperate. Please If someone know something Help me.


One Year ago one guy had this same problem like me now (his mail is
not valid). I spend 6 days to analyse old threads from all mailing
lists and I could not find solution of my problem.

Question is: "How safely run thread from another thread under MFC."
Here is my sample program.
Only you have to do is put it to any MFC application and run.
When you break execution, this will appear:

DBG: Break command failed within 3 seconds.
DBG: Potential deadlock. Soft broken.

Main thread will stop on PumpMessage().
Always one thread from thread list stop in PumpMessage(). probably
main
thread.
it looks like this:
*
->> USER32! 77e11d6b()
CWinThread::PumpMessage() line 821 + 19 bytes
CWnd::RunModalLoop(unsigned long 0x00000004) line 3478 + 19 bytes
CDialog:oModal() line 539 + 12 bytes
CMyApp::InitInstance() line 66 + 11 bytes
*
sometimes it stop after 15 loops, sometimes after 20000 loops.
*
When you press F10 nothing will happen.
Sometimes on thread list are 3 threads, sometimes only main thread.

This same effect I have after few hour of waiting.
Different is only m_mgCur:
After break it is: m_mgCur = 0x0f
After few hours waiting: m_mgCur = 0x219
I have no idea what it means.


My program should:
-- Run Master thread:
-- Master thread should run and stop Slave Thread every 6 seconds.


I have no idea how can I do that.

/*------------------------------------------------------------------------*/
in mfc dialog or mainfrm file:

Master Newobject;
--->> Press Start Button: NewObject.Start_Master();
--->> Press Stop Button: NewObject.Close_Master();

/*------------------------------------------------------------------------*/
file: my_problem.h

class Master{
public:
Master();
~Master();
bool Start_Master();
void Close_Master();
CWinThread* volatile p_Thread_Slave;
CWinThread* volatile p_Thread_Master;
protected:
bool Open_Slave();
void Close_Slave();
static UINT Thread_Slave(LPVOID adr);
static UINT Thread_Master(LPVOID adr);
HANDLE ReqestTerminateEvent_Master;
HANDLE ReqestTerminateEvent_Slave;
};

/*------------------------------------------------------------------------*/
file: my_problem.cpp

Master::Master(){
ReqestTerminateEvent_Master=CreateEvent(NULL,TRUE, FALSE,NULL);
ReqestTerminateEvent_Slave=CreateEvent(NULL,TRUE,F ALSE,NULL);
ResetEvent(ReqestTerminateEvent_Master);
ResetEvent(ReqestTerminateEvent_Slave);
p_Thread_Master=NULL;
p_Thread_Slave=NULL;
}
Master::~Master(){
if (ReqestTerminateEvent_Master!=NULL) {
CloseHandle(ReqestTerminateEvent_Master);
ReqestTerminateEvent_Master=NULL; }
if (ReqestTerminateEvent_Slave!=NULL) {
CloseHandle(ReqestTerminateEvent_Slave);
ReqestTerminateEvent_Slave=NULL; }
}
/*------------------------------------------------------------------------*/
UINT Master::Thread_Master(LPVOID adr) {
Master *LPMaster=(Master*)adr;
if (LPMaster==NULL) return 1;

while(WAIT_OBJECT_0!=WaitForSingleObject(LPMaster->ReqestTerminateEvent_Master,200))
{

if (!LPMaster->Open_Slave()) {ASSERT(false);}
Sleep(3000);
LPMaster->Close_Slave();
Sleep(3000);
}
return 0;
}
UINT Master::Thread_Slave(LPVOID adr) {
Master *LPMaster=(Master*)adr;
if (LPMaster==NULL) { return 1;}

while(WAIT_OBJECT_0!=WaitForSingleObject(LPMaster->ReqestTerminateEvent_Slave,300)){}
return 0;
}
/*------------------------------------------------------------------------*/
bool Master::Open_Slave(){
if (p_Thread_Slave) return false;
ResetEvent(ReqestTerminateEvent_Slave);

p_Thread_Slave=AfxBeginThread(Thread_Slave,this,TH READ_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
if (p_Thread_Slave->m_hThread){
p_Thread_Slave->m_bAutoDelete=false;
p_Thread_Slave->ResumeThread();
} else {
ASSERT(false);
Close_Slave(); return false;
}
return true;
}
void Master::Close_Slave(){
SetEvent(ReqestTerminateEvent_Slave);
if (p_Thread_Slave){
DWORD dwCode;
if ( ::GetExitCodeThread( p_Thread_Slave->m_hThread,
&dwCode )
&& dwCode == STILL_ACTIVE )
{
WaitForSingleObject(
p_Thread_Slave->m_hThread, INFINITE );
}
}
delete p_Thread_Slave; p_Thread_Slave=NULL;
}
/*------------------------------------------------------------------------*/
bool Master::Start_Master(){
if (p_Thread_Master) return false;
ResetEvent(ReqestTerminateEvent_Master);

p_Thread_Master=AfxBeginThread(Thread_Master,this, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED,NULL);
if (p_Thread_Master->m_hThread){
p_Thread_Master->m_bAutoDelete=false;
p_Thread_Master->ResumeThread();
} else {
ASSERT(false);
Close_Slave(); return false;
}
return true;
}
void Master::Close_Master(){
SetEvent(ReqestTerminateEvent_Master);
if (p_Thread_Master){
DWORD dwCode;
if ( ::GetExitCodeThread( p_Thread_Master->m_hThread,
&dwCode )
&& dwCode == STILL_ACTIVE )
{
WaitForSingleObject(
p_Thread_Master->m_hThread, INFINITE );
}
}
delete p_Thread_Master; p_Thread_Master=NULL;
}
/*------------------------------------------------------------------------*/


-----------------
Mariusz Zielinski brainiax@wp.pl
-----------------