CWinThread消息循环错误 [英] CWinThread message loop error
本文介绍了CWinThread消息循环错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
CWinThread
消息循环错误
在我的应用程序中创建一个帖子。
CWinThread
message loop error
create a thread in my application.
m_scanThread = (CScanThread*)AfxBeginThread(RUNTIME_CLASS(CScanThread));
这个线程调用消息循环但是在循环时冻结。当关闭主线程时
继续循环并给出exception.why?救命?
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)
推荐答案
你需要显示更多代码......但是在后面,为什么你有一个带有CWinThread
的消息循环?CWinThread
是一个类,您可以从...派生自己的线程类,这意味着您没有显式创建自己的循环,这是由框架完成的。
看到这个简单的例子 [ ^ ] ...
You need to show more code... but off the back, why do you have a message loop with aCWinThread
? ACWinThread
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[^]...
因为我在CWinThread类中使用了消息循环来连接twain(扫描程序)并获取扫描程序消息。
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;
}
可能有以下两种情况之一
1. br />
There is possible one of two situations
1.
while ( ....GetMessage ...) <---- freezing here
{
}
2.
2.
while ( ....GetMessage ...) <---- not freezing here
{
<---- if there nothing happens, then it is an infinite loop.
The message have to be dispatched, with DispatchMessage
}
要结束消息循环,你应该调用来自当前线程的PostMessage,或来自其他线程的PostThreadMessage,带有消息WM_QUIT
And to end the message loop you should call PostMessage from current thread, or PostThreadMessage from other thread, with message WM_QUIT
这篇关于CWinThread消息循环错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文