Click to See Complete Forum and Search --> : How to Fire Event from Thread


Meka
July 30th, 2005, 03:45 AM
Hi Friends,

Iam new to vc++.
I have fired an event from the thread in the control using
SendMessage();
I am reading data from the USB Device using USBXpress.dll
I fired an event when ever i receive the data from USB Device,And I
want to check the received data in the client side,
I don't know how to catch this event from the client side,and how to check this data in the client...

My thread function follows...

#define WM_THREADFIREEVENT WM_USER+101
ON_MESSAGE(WM_THREADFIREEVENT,FireOnUSBRead)

UINT WorkerThreadProc(LPVOID Param)
{

HWND hwnd=(HWND)Param;
DWORD i=10000;
while(i>1)
{
SI_SetTimeouts( i,10000);
short rbuffer = 0;
DWORD dwbr=1;
DWORD dwbs=0;
SI_STATUS openstatus=SI_SUCCESS;
DWORD d=0;
//To Get Number of Devices Connected
SI_GetNumDevices(&d);
HANDLE m_thUSBDevice = INVALID_HANDLE_VALUE;
for(DWORD j=0;j<d;j++)
{
openstatus=SI_Open(j,&m_thUSBDevice);
}
SI_STATUS r=SI_SUCCESS;
r=SI_Read(m_thUSBDevice,&rbuffer,dwbr,&dwbs);

SI_STATUS fs=SI_FlushBuffers(m_thUSBDevice,0,1);

if(r==SI_SUCCESS && fs==SI_SUCCESS)
{
AfxMessageBox("Success");
SendMessage(hwnd,WM_THREADFIREEVENT,(WPARAM)rbuffer,(LPARAM)NULL);

}
SI_Close(m_thUSBDevice);
i++;
}
return TRUE;

}

void CUSBXPressCtrlCtrl::StartThread()
{
// TODO: Add your dispatch handler code here
AfxBeginThread(WorkerThreadProc,this,THREAD_PRIORITY_LOWEST,0,0,NULL);

}


Please help me....
Regards,
Sindhu

Andreas Masur
July 30th, 2005, 04:45 AM
[ Redirected thread ]

D_Drmmr
July 30th, 2005, 04:47 AM
Please make only one thread for each problem you have (not 3 times the same).
Please use code tags to make your code more readable. already done

You can catch the WM_THREADFIREEVENT message in the FireOnUSBRead function, since you specified this in the ON_MESSAGE macro.

Note that AfxMessageBox(...) does not return until the user clicks OK. Also the SendMessage function does not return until the message is handled (i.e. the function 'FireOnUSBRead' has exited). Hence, you can safely access data that is stored in your thread procedure, by passing a pointer to that data with the message, though this is not necessarily the best way to do it.

Andreas Masur
July 30th, 2005, 04:48 AM
Take a look at the following FAQ:

How to access UI elements from a thread in MFC? (http://www.codeguru.com/forum/showthread.php?t=312454)


#define WM_THREADFIREEVENT WM_USER+101


Just a small remark...in general you can use both 'WM_USER' or 'WM_APP' for user-defined messages. However, you should rather use 'WM_APP' as a base for your user-defined messages since it provides less problems. Windows itself uses the range 'WM_USER + x' for several messages therefore you can run into conflict problems pretty easy...

humptydumpty
July 30th, 2005, 06:59 AM
just use
::SendMessage() in the thread to fire a event

like i am calling a Dialog and from thread sending a message to destroy that dialog

static unsigned long __stdcall FnBackupThread(LPVOID lpParam)
{
CmBackup2GoDlg *pParent = (CmBackup2GoDlg *)lpParam;
pParent->m_objProgressBar.SendMessage(WM_DESTROY,0,0);
}