Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
How do you post messages from a thread when none of your classes are CWinThread-derived?

BEGIN_MESSAGE_MAP(CMyClass, CDialog)
    ON_MESSAGE(WM_MYMESSAGE, &CMyClass::OnMyMessage)
END_MESSAGE_MAP()

CMyClass::CMyClass()
{
	m_pThread = AfxBeginThread((AFX_THREADPROC)MyLoop, NULL);
}

CMyClass::~CMyClass()
{
}

void CMyClass::MyLoop()
{
	while(m_bContinue)
	{
		::PostMessageA(/*HWnd*/,WM_MYMESSAGE, NULL, NULL); // ???
		Sleep(1000);
	}
}

void CMyClass::OnMyMessage()
{
	// do something interesting
}


I want to post a message from this loop that gets kicked off when a new thread starts. In the example, I want to post a message every second to the main thread so OnMyMessage() can do something interesting every second.

I keep getting told that PostMessage is not static so I can't call it from my thread. However, I think I read on CP that you have to use PostMessage to send messages between threads if the threads are not CWinThread.

=================================================
So I got as far as using ::PostMessage thanks to CPallini, but now I have a new problem. How do I get the correct HWnd? I forced it to compile by setting a NULL. I don't know for sure if that's the problem but the message map macro failed. Something about not being able to convert between void (_thiscall CMyClass::*)(void) and void(_thiscall CWnd::*)(WPARAM, LPARAM).

Please help!

Thanks.
Posted
Updated 15-Apr-10 23:19pm
v3

1 solution

You may call the PostMessage function, by means of the scope resolution operator ::
C++
void CMyClass::MyLoop()
{
    while(m_bContinue)
    {
        ::PostMessage(...); 
        Sleep(1000);
    }
}

:)
 
Share this answer
 
v2

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