Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ CWinThread
CWinThread message loop error
 
create a thread in my application.
m_scanThread = (CScanThread*)AfxBeginThread(RUNTIME_CLASS(CScanThread)); 
this thread call message loop but freeze while loop.when close the main thread
continue while loop and give exception.why ? help?
MSG Msg; 
BOOL bRet;           
 
while (bRet = ::GetMessage((LPMSG)&Msg,NULL, 0, 0) != 0)
Posted 13-Dec-12 4:20am
werderu2.1K
Edited 13-Dec-12 5:21am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need to show more code... but off the back, why do you have a message loop with a CWinThread? A CWinThread is a class that you can derive your own thread class from... meaning you don't explicitly create your own loop, that's done by the framework.
 
See this simple example[^]...
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

because i used message loop in CWinThread class for connect twain (scanner) and get scanner messages.
 
m_scanThread = (CScanThread*)AfxBeginThread(RUNTIME_CLASS(CScanThread));
//m_scanThread->CreateThread();
m_scanThread->PostThreadMessage(WM_SCAN,(WPARAM)pScannerListCombo->GetItem(sel), (LPARAM)1);
 

 
void CScanThread::Scan(WPARAM wParam, LPARAM lParam)
{	
	try
	{ 
		this->connectDSM();
	//fill data source
	this->m_pDataSource = NULL;
	pTW_IDENTITY pID = NULL;
	int   index = 0;
	while( NULL != (pID =  this->getDataSource(index)) )
	{  
		if( (LPWSTR)wParam == CString(pID->ProductName) )
		{
			this->m_pDataSource = pID;
			break;
		}
		index++;
	 } 
 
		m_bKill = FALSE;	// Indicates that the UI-thread is activated and it is used for displaying the dialog box.
		m_bRunning = TRUE;
 
		if(this->m_DSMState > 4)
			return; // Already Enabled
		if(this->m_pDataSource != NULL)
			this->loadDS(this->m_pDataSource->Id);
 
		if(this->enableDS(NULL,false))
		{		
			MSG Msg; 
			BOOL bRet;
		//CMainFrame* pp =	(CMainFrame*)(AfxGetApp()->m_pMainWnd);
		
//freeze this point
			while (bRet = ::GetMessage((LPMSG)&Msg,NULL, 0, 0) != 0)
			{
 
				TW_EVENT twEvent = {0};
				twEvent.pEvent = (TW_MEMREF)&Msg;
				twEvent.TWMessage = MSG_NULL;
				TW_UINT16  twRC = TWRC_NOTDSEVENT;
				twRC = _DSM_Entry(this->getAppIdentity(),
								  this->m_pDataSource,
							      DG_CONTROL,
							      DAT_EVENT,
							      MSG_PROCESSEVENT,
							     (TW_MEMREF)&twEvent);
 
			  //TODO:
			  //if(!gUSE_CALLBACKS && twRC==TWRC_DSEVENT)
			  if( twRC==TWRC_DSEVENT)
			  {
				// check for message from Source
				switch (twEvent.TWMessage)
				{
				  case MSG_XFERREADY:
				  case MSG_CLOSEDSREQ:
				  case MSG_NULL:
					this->m_DSMessage = twEvent.TWMessage;
					break;
 
				  case MSG_CLOSEDSOK:
					TRACE(L"\nError - MSG_CLOSEDSOK in MSG_PROCESSEVENT loop for Scan\n");
					break;
 
				  default:
					TRACE(L"\nError - Unknown message in MSG_PROCESSEVENT loop for Scan\n");
					break;
				}
			  }
				else
				{
					TranslateMessage ((LPMSG)&Msg);
					DispatchMessage ((LPMSG)&Msg);
				}
 
			}
  
			//::SendMessage(this->m_hWnd , WM_GETMESSAGE , (WPARAM) m_pDataSource , 0);

		//			// If we are using callbacks, there is nothing to do here except sleep
		//// and wait for our callback from the DS.  If we are not using them, 
		//// then we have to poll the DSM.
		//	MSG Msg;
		//	//while (GetMessage((LPMSG)&Msg, NULL, 0, 0))
	
		//	while (PeekMessage((LPMSG)&Msg, NULL, 0, 0,PM_REMOVE))
		//	{

		//		if (!AfxGetApp ()->PumpMessage ()) { 
		//			 ::PostQuitMessage (0); 
		//			 return; 
		//		} 

		//		TW_EVENT twEvent = {0};
		//		twEvent.pEvent = (TW_MEMREF)&Msg;
		//		twEvent.TWMessage = MSG_NULL;
		//		TW_UINT16  twRC = TWRC_NOTDSEVENT;
		//		twRC = _DSM_Entry( getAppIdentity(),
		//						   m_pDataSource,
		//						   DG_CONTROL,
		//						   DAT_EVENT,
		//						   MSG_PROCESSEVENT,
		//						   (TW_MEMREF)&twEvent);
		//		 if(m_DSMessage == MSG_XFERREADY)
		//		 {
		//			 int i =0;
		//	 
		//		}
		//		else
		//		{
		//			TranslateMessage ((LPMSG)&Msg);
		//			DispatchMessage ((LPMSG)&Msg);
		//		}
		//	}

		//	//// Scan is done, disable the ds, thus moving us back to state 4 where we
		//	//// can negotiate caps again.
		//	//this->disableDS();
		}
	}
    catch(...)
	{
 
	}
	m_bKill = TRUE;
	m_bRunning = FALSE;
}
  Permalink  
Comments
pwasser at 13-Dec-12 21:23pm
   
Don't post questions as answers. It makes the whole thread impossible to follow.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

There is possible one of two situations
 
1.
while ( ....GetMessage ...) <---- freezing here
{
}
2.
while ( ....GetMessage ...) <---- not freezing here
{
   <---- if there nothing happens, then it is an infinite loop.
   The message have to be dispatched, with DispatchMessage
}
 
And to end the message loop you should call PostMessage from current thread, or PostThreadMessage from other thread, with message WM_QUIT
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

i call with thread with PostThreadMessage from main thread.
 

m_scanThread = (CScanThread*)AfxBeginThread(RUNTIME_CLASS(CScanThread));
//m_scanThread->CreateThread();
m_scanThread->PostThreadMessage(WM_SCAN,(WPARAM)pScannerListCombo->GetItem(sel), (LPARAM)1);
 
while ( ....GetMessage ...) <---- freezing here
{
}
 

if i send WM_QUIT command then kill it.i dont want to kill thread.
  Permalink  
Comments
armagedescu at 14-Dec-12 6:09am
   
Nope. If you send WM_QUIT you do not kill the thread. Sending WM_QUIT does not kill the thread. It causes the GetMessage to return 0 and exit the message loop. So the thread is not killed. It is executed successfully until returning from thread function. If you want to kill a thread, you have to call TerminateThread.
But if the GetMessage freezes, this means there are no messages in message loop. But it is not likely that is freezing inside GetMessage. Try using PickMessage instead, to see more exactly that happens.
armagedescu at 14-Dec-12 6:20am
   
By the way, I am not sure, but you have to see if there exists any other message loops in the applications.
nv3 at 25-Jun-13 9:45am
   
Please use the "Improve Question" button to add more content to your original post, as already mentioned for your previous "solution". It makes it hard for readers to follow the thread and it lowers your chances to receive an answer. Other readers will see that your question has already a great number of answers and won't even take a look at your question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 25 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100