Re: Worker thread in VC++ 6

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxxxxxxxxxxxxxxx
I 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@xxxxxxxxxxx
I 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;
}






.



Relevant Pages

  • Re: serialPort receive problem
    ... That would be expected, as the serial port data could not have been transmitted, and a response received, in the time that you have allowed between the Write command and the ReadExisting call. ... Every thing works fine except whenever I receive data I cannot display ...
    (microsoft.public.dotnet.languages.vb)
  • Re: SetWindowsHookEx, just one question;
    ... have a NULL display driver, ... forget about keyboard hooks and just write ... typedef LRESULT (int code, WPARAM wParam, LPARAM ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: cross-platform serial api for gforth
    ... serial port on Unix and Windows? ... The project we are working on is an interactive display for a device ... way for me to use Gforth on a Winblows machine? ...
    (comp.lang.forth)
  • Re: Unable to read the string
    ... Arun wrote: ... string from the serial port but i am not able to and this is my ... # Display messages if enabled ... print $result if $display; ...
    (perl.beginners)
  • Re: cross-platform serial api for gforth
    ... implement serial port I/O and the top level code be the same. ... If you start from what Windows and Unices offer at the raw API ... the serial port stream is just that, ... emulation for display of the data that is updated in real time. ...
    (comp.lang.forth)