Re: Worker thread in VC++ 6
- From: "Tom Serface" <tom.nospam@xxxxxxxxxxxxx>
- Date: Mon, 18 Feb 2008 22:11:13 -0800
OK. Then what I'd do is just post a message when you think something needs to be displayed. For example, you can create a WM_APP message and post that:
I typically create messages like:
#define WM_UPDATE_STATUSMESSAGE (WM_APP + 200)
#define WM_UPDATE_PROGRESS (WM_APP + 201)
#define WM_UPDATE_ALL (WM_APP + 202)
And when I create thread object I have a function like the following that I set in the thread from the main window before any status could happen.
void CMyThread::GUIWindow(CWnd *pWnd)
{
m_pStatusWnd = pWnd;
}
void CMyThread::UpdateStatus()
{
if(m_pStatusWnd)
m_pStatusWnd->PostMessage(WM_UPDATE_STATUSMESSAGE,NULL,NULL);
}
For example, you could start the thread in mainframe somewhere:
CMyThread thread;
thread.GUIWindow(this);
Then you have to have a handler function:
In mainfrm.h
LRESULT OnUpdateStatus(WPARAM wParam, LPARAM lParam);
In mainfrm.cpp
ON_MESSAGE(WM_UPDATE_STATUSMESSAGE, OnUpdateStatus)
LRESULT CMainframe::OnUpdateStatus(WPARAM wParam, LPARAM lParam)
{
// Thread wants us to do something
return 0L;
}
You could call a function like this every so often in your worker thread when you want to update the UI. Then you could have the UI fish the current data out of the thread or global data you're using.
Tom
"Kahlua" <kahlua@xxxxxxxxxx> wrote in message news:7hruj.7121$_T3.979@xxxxxxxxxxx
I did not plan on using AfxMessageBox in the final app.
It is just there to test if data from serial port is read corectly.
"Tom Serface" <tom.nospam@xxxxxxxxxxxxx> wrote in message news:1E9A58F3-83E7-44C0-B4CA-CB38DC0130D8@xxxxxxxxxxxxxxxxI would use PostMessage() and send the value to the UI thread and display it there. It might work, but I would not call AfxMessageBox from a worker thread.
Once you send it to a message that is handled by your UI thread (main thread) then you can do just about anything with the data (display, log to file, accumulate, etc.)
Tom
"Kahlua" <kahlua@xxxxxxxxxx> wrote in message news:9_puj.8413$eg3.1327@xxxxxxxxxxxI have the following code below that apears to be working.
I have not included all the serial port initialization here.
When I receive a 30h or 31h or 32h into the serial port it does display the correct "Got ##h" message.
Now how do I get the data (Bar[0]) to "UpdateDisplay()" ?
Bar[] is a global varible;
I checked the task manager and I do not seems to be wasting any CPU cycles.
Thanks
=================================================================
void CMartin1Dlg::UpdateDisplay()
{
m_BarChart.SetNumData(33);
m_BarChart.SetData(1,Bar[0]);
m_BarChart.Invalidate();
}
UINT WorkerThreadProc( LPVOID Param ) //Sample function for using in AfxBeginThread
{
int DataByte=0x00;
int Count=0;
loop:
while(Count == 0){
Count = Port.BytesInReceiveBuffer();
Sleep(100);
}
DataByte=Port.GetByte(18);
if (DataByte == 0x30){
Bar[0]=0;
AfxMessageBox("Got 30h");
}
if (DataByte == 0x31){
Bar[0]=10;
AfxMessageBox("Got 31h");
}
if (DataByte == 0x32){
Bar[0]=20;
AfxMessageBox("Got 32h");
}
goto loop;
return TRUE;
}
.
- References:
- Worker thread in VC++ 6
- From: Kahlua
- Re: Worker thread in VC++ 6
- From: Tom Serface
- Re: Worker thread in VC++ 6
- From: Kahlua
- Worker thread in VC++ 6
- Prev by Date: ListView control insists on displaying "label tip"?
- Next by Date: Re: modeless dialog
- Previous by thread: Re: Worker thread in VC++ 6
- Next by thread: Re: Worker thread in VC++ 6
- Index(es):
Relevant Pages
|