Click here to Skip to main content
15,900,325 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: VS.NET Classwizard Pin
Anthony_Yio14-May-04 1:26
Anthony_Yio14-May-04 1:26 
GeneralTurning off Runtime checks in VC7 Pin
Anonymous13-May-04 22:10
Anonymous13-May-04 22:10 
GeneralRe: Turning off Runtime checks in VC7 Pin
Anthony_Yio14-May-04 1:57
Anthony_Yio14-May-04 1:57 
GeneralMain MFC and its Threads Pin
sweep12313-May-04 22:00
sweep12313-May-04 22:00 
GeneralRe: Main MFC and its Threads Pin
jmkhael13-May-04 23:23
jmkhael13-May-04 23:23 
GeneralRe: Main MFC and its Threads Pin
sweep12314-May-04 0:26
sweep12314-May-04 0:26 
GeneralRe: Main MFC and its Threads Pin
Jitendra gangwar14-May-04 0:47
Jitendra gangwar14-May-04 0:47 
GeneralRe: Main MFC and its Threads Pin
Roger Stoltz14-May-04 1:30
Roger Stoltz14-May-04 1:30 
sweep123 wrote:
Now the thread is started by the main MFC program via:-
m_DisplayTextThread = ::AfxBeginThread(CTestGUI1Dlg::DisplayTextThread, (LPVOID) &m_DisplayThreadData);
and passed the pointer (this) of the main MFC application in the structure m_DisplayThreadData in order for it to access the methods/properties of the main MFC application that require updating.


Regarding thread startup...
Without exceptions I pass the this-pointer when spawning threads since I consider it good modeling and encapsulation. It also simplifies my life since I can use PostMessage from my thread in the way that you want/need. My header files looks something like this:
class CMyDialog : public CDialog //or another suitable CWnd derived base class
{
    .....
protected:
    static UINT MyThreadFn( LPVOID pThis ) { return ((CMyDialog*)pThis)->MyThreadFn(); }
    UINT MyThreadFn(); // The thread controlling function
    volatile BOOL m_bStopMyThread;
    CWinThread* m_pMyThread;
    ....
};
This way I get access to member variables from the thread function MyThread() without polluting the code with a lot of type casting.

sweep123 wrote:
My question is - what is the scheme to use for this type of activity, where the GUI needs to reflect data received, but when the receive processing is done in a different thread.

How would this be done? I have looked up PostThreadMessage but dont see how to use it.


If you use the thread solution above you can forget about PostThreadMessage and use PostMessage instead from your thread since the thread function is a member of a CWnd derived class.
All you need is to define a message ID for your message, declare a message handler, add a message entry in the message map and implement your message handler. In code it would look something like this for the header file:
#define UWM_MY_MESSAGE (WM_APP + 1)
class CMyClass : public CDialog
{
    ....
    afx_msg LRESULT OnMyMessage( WPARAM wParam, LPARAM lParam );
    ....
};
...and for the cpp file:
....
BEGIN_MESSAGE_MAP(CMyClass, CDialog)
    ....
    ON_MESSAGE( UWM_MY_MESSAGE, OnMyMessage )
    ....
END_MESSAGE_MAP()
....
....
LRESULT CMyClass::OnMyMessage( WPARAM wParam, LPARAM lParam )
{
    // Do whatever you need to do here
    return 0;
}
If you want you can send the length and a pointer to data from your thread using the two parameters wParam and lParam. The memory allocation is done in your thread and deallocation is done in the message handler. Something like this for the thread:
UINT CMyClass::MyThreadFn()
{
    WORD wLength; // The length of the received data
    BYTE* pData;  // Pointer to the received data
    ....
    while( !m_bStopMyThread )
    {
        ....
        pData = new BYTE[wLength];
        // Here should be some code that copies the data to pData e.g. memcpy( pByte, ...., wLength )
        PostMessage( UWM_MY_MESSAGE, (WPARAM)wLength, (LPARAM)pByte );
        ....
    }
    ....
}
and for the message handler...
LRESULT CMyClass::OnMyMessage( WPARAM wParam, LPARAM lParam )
{
    WORD wLength = (WORD)wParam;
    BYTE* pData = (BYTE*)lParam;
    // Do whatever you need to do with your data, update controls and so on
    ....
    delete pData;
    return 0;
}

Hope this helps
--
Roger
GeneralRe: Main MFC and its Threads Pin
sweep12314-May-04 2:02
sweep12314-May-04 2:02 
GeneralRe: Main MFC and its Threads Pin
Roger Stoltz14-May-04 3:38
Roger Stoltz14-May-04 3:38 
GeneralRe: Main MFC and its Threads Pin
Grahamfff14-May-04 11:39
Grahamfff14-May-04 11:39 
GeneralRe: Main MFC and its Threads Pin
Roger Stoltz16-May-04 13:13
Roger Stoltz16-May-04 13:13 
GeneralRe: Main MFC and its Threads Pin
sweep12317-May-04 2:00
sweep12317-May-04 2:00 
QuestionHow to use Amazon Web Services.. Pin
Sumit Kapoor13-May-04 21:56
Sumit Kapoor13-May-04 21:56 
AnswerRe: How to use Amazon Web Services.. Pin
Anthony_Yio14-May-04 1:46
Anthony_Yio14-May-04 1:46 
GeneralCListCtrl and CImageList question Pin
DaFrawg13-May-04 21:52
DaFrawg13-May-04 21:52 
GeneralRe: CListCtrl and CImageList question Pin
DaFrawg23-May-04 22:43
DaFrawg23-May-04 22:43 
Questionwhich Project type to use? Pin
frumm13-May-04 21:48
frumm13-May-04 21:48 
AnswerRe: which Project type to use? Pin
Maxwell Chen13-May-04 21:54
Maxwell Chen13-May-04 21:54 
AnswerRe: which Project type to use? Pin
Cedric Moonen13-May-04 23:01
Cedric Moonen13-May-04 23:01 
AnswerRe: which Project type to use? Pin
hasansheik14-May-04 0:18
hasansheik14-May-04 0:18 
GeneralRe: which Project type to use? Pin
frumm14-May-04 6:29
frumm14-May-04 6:29 
GeneralDVD Class Pin
foxele13-May-04 21:23
foxele13-May-04 21:23 
GeneralRe: DVD Class Pin
David Crow14-May-04 4:29
David Crow14-May-04 4:29 
Generaltooltips in CTreeCtrl to show some other info rather than the node name Pin
SVPG13-May-04 21:22
SVPG13-May-04 21:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.