Click here to Skip to main content
15,913,709 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: CString insertion operator faulty? Pin
toxcct14-May-04 0:00
toxcct14-May-04 0:00 
AnswerRe: CString insertion operator faulty? Pin
Anand Paranjpe14-May-04 0:46
Anand Paranjpe14-May-04 0:46 
AnswerRe: CString insertion operator faulty? Pin
David Crow14-May-04 2:44
David Crow14-May-04 2:44 
GeneralRe: CString insertion operator faulty? Pin
Mike Dimmick14-May-04 5:42
Mike Dimmick14-May-04 5:42 
GeneralRe: CString insertion operator faulty? Pin
David Crow14-May-04 5:46
David Crow14-May-04 5:46 
GeneralRe: CString insertion operator faulty? Pin
Mike Dimmick14-May-04 9:06
Mike Dimmick14-May-04 9:06 
GeneralRe: CString insertion operator faulty? Pin
David Crow14-May-04 9:10
David Crow14-May-04 9:10 
AnswerRe: CString insertion operator faulty? Pin
shea-c415-Oct-09 12:14
shea-c415-Oct-09 12:14 
GeneralINTERFACING THE PABX WITH A COMPUTER Pin
Wisdom200413-May-04 23:49
Wisdom200413-May-04 23:49 
GeneralRe: INTERFACING THE PABX WITH A COMPUTER Pin
Michael P Butler14-May-04 0:24
Michael P Butler14-May-04 0:24 
GeneralTake benefit of serialize but... Pin
doctorpi13-May-04 23:43
doctorpi13-May-04 23:43 
GeneralRe: Take benefit of serialize but... Pin
doctorpi13-May-04 23:48
doctorpi13-May-04 23:48 
GeneralACCESSING DATABASES USING DIALOGUE FORMS INSTEAD OF DOCUMENT FORMS/VIEWS Pin
Wisdom200413-May-04 23:39
Wisdom200413-May-04 23:39 
GeneralGDI+ Pin
CuongVT13-May-04 23:36
CuongVT13-May-04 23:36 
GeneralVS.NET Classwizard Pin
RChin13-May-04 22:50
RChin13-May-04 22:50 
GeneralRe: VS.NET Classwizard Pin
Maxwell Chen13-May-04 22:54
Maxwell Chen13-May-04 22:54 
GeneralRe: VS.NET Classwizard Pin
RChin13-May-04 22:59
RChin13-May-04 22:59 
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

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.