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->PostThreadMessage(WM_SCAN,(WPARAM)pScannerListCombo->GetItem(sel), (LPARAM)1);
void CScanThread::Scan(WPARAM wParam, LPARAM lParam)
{
try
{
this->connectDSM();
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;
m_bRunning = TRUE;
if(this->m_DSMState > 4)
return;
if(this->m_pDataSource != NULL)
this->loadDS(this->m_pDataSource->Id);
if(this->enableDS(NULL,false))
{
MSG Msg;
BOOL bRet;
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);
if( twRC==TWRC_DSEVENT)
{
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);
}
}
}
}
catch(...)
{
}
m_bKill = TRUE;
m_bRunning = FALSE;
}