Click to See Complete Forum and Search --> : Thread + AfxGetMainWnd()
Tischnoetentoet
November 1st, 2004, 02:30 PM
Hi all,
I am creating a thread (CWinThread class);
I am only using this code:
BOOL CMyThread::InitInstance()
{
// Get parent handle
CWnd * pWnd = AfxGetMainWnd();
// Send message that we are ready
pWnd->PostMessage(WM_TASK_COMPLETE, (WPARAM)TASK_MYTHREAD);
return TRUE;
}
The problem is that pWnd is null.
I call this function like this at the end (and tried even after) OnInitDialog():
CMyThread * pMyThread = (CMyThread*)AfxBeginThread(RUNTIME_CLASS(CMyThread));
Any idea where the problem is?
TSYS
November 1st, 2004, 02:37 PM
I believe the main window is in CWinThread::m_pMainWnd. I'm actually a little surprised that AfxGetMainWnd() didn't assert.
Tischnoetentoet
November 1st, 2004, 02:51 PM
yes, but when I call the function AfxGetMainWnd(), I get that member variable.
Or am I tottaly wrong now??
TSYS
November 1st, 2004, 03:31 PM
No, you're right. I'm wrong (again). You have to assign a value to CWinThread::m_pMainWnd. The following quote is from this link (http://www.murrayc.com/learning/windows/multithreading.shtml)
Threads may have a window. You may create a window in InitInstance() and assign it to CWinThread's m_pMainWnd member variable. All messages for that window will then be sent to that thread. You will have, in effect, created a window running in its own thread. Note that this technique can often lead to poor performance because sending a windows messages may halt the sender until the ::SendMessage() function returns.
Tischnoetentoet
November 2nd, 2004, 01:23 AM
I think it's not meant to be filled by the programming. I think it should be done automatically.
I've seen applications that worked perfectly and automatically!
cilu
November 2nd, 2004, 04:01 AM
If AfxGetMainWnd() is called from the application's primary thread, it returns the application's main window according to the above rules. If the function is called from a secondary thread in the application, the function returns the main window associated with the thread that made the call.
If your thread has no window attached, it returns NULL.
Tischnoetentoet
November 2nd, 2004, 04:45 AM
The thing I want to realise is this:
I have 1 dialog. From this dialog, I start several threads (CWinThread).
These threads must send a message when they are ready to the main dialog.
Then I send a message to the thread that he can stop:
pThread1->PostMessage(WM_QUIT, 0, 0);
So the thread itself does not have a window. But how can I send a message to the main dialog (the one that started the thread)?
ovidiucucu
November 2nd, 2004, 05:15 AM
The example I posted for you HERE (http://www.codeguru.com/forum/showthread.php?t=316181) works.
However, to assure that AfxGetMainWnd returns what you want, do something like this:
void CMyDialog::WhateverFunction()
{
// ...
m_pThread1 = (CThread1*)AfxBeginThread(RUNTIME_CLASS(CThread1),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
m_pThread1->m_pMainWnd = this;
m_pThread1->m_pActiveWnd = this;
m_pThread1->ResumeThread();
}
Tischnoetentoet
November 2nd, 2004, 11:20 AM
:thumb: works perfect :)
Andreas Masur
November 3rd, 2004, 02:42 AM
[ Moved thread ]
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.