Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 13-Dec-12 5:21am
v2

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[^]...
 
Share this answer
 
v2
Comments
[no name] 14-Dec-12 2:15am    
Yoy are right
because i used message loop in CWinThread class for connect twain (scanner) and get scanner messages.

C#
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;
}
 
Share this answer
 
Comments
[no name] 13-Dec-12 21:23pm    
Don't post questions as answers. It makes the whole thread impossible to follow.
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
 
Share this answer
 
i call with thread with PostThreadMessage from main thread.


C#
m_scanThread = (CScanThread*)AfxBeginThread(RUNTIME_CLASS(CScanThread));
//m_scanThread->CreateThread();
m_scanThread->PostThreadMessage(WM_SCAN,(WPARAM)pScannerListCombo->GetItem(sel), (LPARAM)1);


C#
while ( ....GetMessage ...) <---- freezing here
{
}



if i send WM_QUIT command then kill it.i dont want to kill thread.
 
Share this answer
 
Comments
armagedescu 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 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 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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900