Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

My embedded application is displaying some real time values and some scrolling text.
I am using a CWinThread class to poll my comms port and get the values, and the main UI thread is managing the scrolling.

I am trying to poll at specific intervals, and have tried to use a timer, but this gets gobbled up by the main UI class and doesn't reach the destination unless I specifically include the main UI HWND, which then causes a delay in the main UI update.

So, I chose to use PostThreadMessage in my comms thread to run my timer, but even using the threadID acquired at thread creation, my message never reaches the destination.
I have used IMPLEMENTM and DECLARE _DYNAMIC, and have declared my message maps

C++
IMPLEMENT_DYNAMIC(CMyThread, CWinThread)

BEGIN_MESSAGE_MAP(CMyThread, CWinThread)
    //ON_WM_TIMER ( )
    ON_MESSAGE(WM_APP+15, OnTimer)
END_MESSAGE_MAP()

afx_msg void CADCValuesThread::OnTimer(UINT nIDEvent)
{
    readComms();
}

void readComms()
{
    //read my comms port
    ::PostThreadMessage(m_dwThreadID, WM_APP+15, 0, 0);
}


All to no avail.

Any help as to why my message is not reaching my thread, even though the thread is happily running?

Thanks

Tony
Posted
Updated 1-Dec-11 14:45pm
v4
Comments
Sergey Alexandrovich Kryukov 30-Nov-11 12:38pm    
Please tag your language and platform. Visual Studio? What version? What's "eVC4.0"? Just tag it correctly.
--SA
[no name] 1-Dec-11 20:45pm    
EDIT: added "code" tag

I have - eVC4.0 is embedded Visual C++ 4.0, it's an option offered in the tags, not one I made up.
 
Share this answer
 
Comments
Albert Holguin 1-Dec-11 9:54am    
Don't post updates as solutions please...
PostThreadMessage() requires a corresponding ON_THREAD_MESSAGE() entry in your message map.

Your posted code doesn't make much sense though, for example, is readComms() a global C-style function? If so, how is it accessing a member m_dwThreadID?

Is CADCValuesThread::OnTimer() in your main GUI thread (since the name is different than your other thread)?

[edit]
A couple of other things relating to your CWinThread:
0. Make sure it's running when you call AfxBeginThread (i.e. you have to ResumeThread() if you CREATE_SUSPENDED).
1. CWinThread::InitInstance() MUST be overridden to return a value (0, the default indicates the thread did not start due to an error).
[/edit]
 
Share this answer
 
v2
The IMPLEMENT_DYNAMIC and BEGIN_MESSAGE_MAP Macros are used to help define the Window Procedure, they do not actually setup the processing of the message queue.

Make sure that you call the Run method of your thread that you have created to look for this message in the background.

C++
int CWinThread::Run()


This is where the message pump is processed in an MFC application. If you do not call this function, the messages will just stack up in the message queue for your thread and never get processed.
 
Share this answer
 
Comments
Albert Holguin 1-Dec-11 10:16am    
What? You shouldn't call Run() directly, that's where the internal message pump resides.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900