Posting messages to the main window from worker threads 
Author Message
 Posting messages to the main window from worker threads

Hello all. I've read a lot of confusing, contradictory advice regarding
how to post a message from a worker thread to a program's main window.

Joe Newcomer's opinion (from the website):
Never call AfxGetMainWnd() from a worker thread because a worker thread
has no main window and the function will return NULL. Joe then goes on to
recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
a public place, then use it (an alternative method that I've seen Joe use
in the newsgroup is to pass the CWnd* in as the thread's parameter).

Scott McPhillips (and www.mvps.org) opinion:
Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member, and
call the Win32 API's PostMessage function instead of using the pointer
that was returned from AfxgetMainWnd and CWnd::PostMessage.

My question is this: Which of these opinions is correct? Also, if I
create my MFC project with the AppWizard, why not just access the
m_pMainWnd member of the global variable theApp (which is the main thread
object) to get the pointer to the application's main window? Of course,
the question as to whether or not to use the pointer or the m_hWnd member
remains.

Thanks,
Peter Huber



Fri, 11 Nov 2005 20:28:03 GMT  
 Posting messages to the main window from worker threads
I reckon it's better style to pass the CWnd* across to the thread
because it tends to make the code more general and hence more reuseable.
In the future, you might be able to use the code and post your results
to another window
Quote:

> Hello all. I've read a lot of confusing, contradictory advice regarding
> how to post a message from a worker thread to a program's main window.

> Joe Newcomer's opinion (from the website):
> Never call AfxGetMainWnd() from a worker thread because a worker thread
> has no main window and the function will return NULL. Joe then goes on to
> recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
> a public place, then use it (an alternative method that I've seen Joe use
> in the newsgroup is to pass the CWnd* in as the thread's parameter).

> Scott McPhillips (and www.mvps.org) opinion:
> Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member, and
> call the Win32 API's PostMessage function instead of using the pointer
> that was returned from AfxgetMainWnd and CWnd::PostMessage.

> My question is this: Which of these opinions is correct? Also, if I
> create my MFC project with the AppWizard, why not just access the
> m_pMainWnd member of the global variable theApp (which is the main thread
> object) to get the pointer to the application's main window? Of course,
> the question as to whether or not to use the pointer or the m_hWnd member
> remains.

> Thanks,
> Peter Huber



Fri, 11 Nov 2005 21:55:02 GMT  
 Posting messages to the main window from worker threads
Hello Peter

For a safe (but kinda crude***) way to get the main window you could try :-

AfxGetApp()->m_pMainWnd

The basic issue with calling AfxGetMainWnd from a thread is that it might
return the main window for that thread which *might not* be the main window
for your application

--

Regards,
Nish [VC++ MVP]

*** crude in the sense - gurus like Joe, Scott might not really appreciate
it when you access data members directly :-)


Quote:
> Hello all. I've read a lot of confusing, contradictory advice regarding
> how to post a message from a worker thread to a program's main window.

> Joe Newcomer's opinion (from the website):
> Never call AfxGetMainWnd() from a worker thread because a worker thread
> has no main window and the function will return NULL. Joe then goes on to
> recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
> a public place, then use it (an alternative method that I've seen Joe use
> in the newsgroup is to pass the CWnd* in as the thread's parameter).

> Scott McPhillips (and www.mvps.org) opinion:
> Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member, and
> call the Win32 API's PostMessage function instead of using the pointer
> that was returned from AfxgetMainWnd and CWnd::PostMessage.

> My question is this: Which of these opinions is correct? Also, if I
> create my MFC project with the AppWizard, why not just access the
> m_pMainWnd member of the global variable theApp (which is the main thread
> object) to get the pointer to the application's main window? Of course,
> the question as to whether or not to use the pointer or the m_hWnd member
> remains.

> Thanks,
> Peter Huber



Fri, 11 Nov 2005 22:58:08 GMT  
 Posting messages to the main window from worker threads

Quote:

> Hello all. I've read a lot of confusing, contradictory advice regarding
> how to post a message from a worker thread to a program's main window.

> Joe Newcomer's opinion (from the website):
> Never call AfxGetMainWnd() from a worker thread because a worker thread
> has no main window and the function will return NULL. Joe then goes on to
> recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
> a public place, then use it (an alternative method that I've seen Joe use
> in the newsgroup is to pass the CWnd* in as the thread's parameter).

> Scott McPhillips (and www.mvps.org) opinion:
> Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member, and
> call the Win32 API's PostMessage function instead of using the pointer
> that was returned from AfxgetMainWnd and CWnd::PostMessage.

> My question is this: Which of these opinions is correct? Also, if I
> create my MFC project with the AppWizard, why not just access the
> m_pMainWnd member of the global variable theApp (which is the main thread
> object) to get the pointer to the application's main window? Of course,
> the question as to whether or not to use the pointer or the m_hWnd member
> remains.

> Thanks,
> Peter Huber

Calling the MFC CWnd::PostMessage has exactly the same effect as calling
the Win32 API ::PostMessage with the HWND.  Both ways are correct: The
MFC version just uses m_hWnd to call the API version.

I wrote that mvps.org FAQ answer.  It says to use the HWND for
pedagogical reasons, and to be immune to possible changes in MFC.  The
whole point of that FAQ is to help people who do not know that a CWnd*
to a window in another thread will not work in many situations.  So I
felt it was dangerous to recommend a CWnd* to such a reader.

As for getting the HWND or CWnd* in the thread, Joe is right that
AfxGetMainWnd will return NULL in a worker thread, and the FAQ is
wrong.  The phrase "or you can get it with AfxGetMainWnd()->m_hWnd from
within worker
threads" should be deleted.  I'll see what I can do about that :-(

--
Scott McPhillips [VC++ MVP]



Fri, 11 Nov 2005 23:28:26 GMT  
 Posting messages to the main window from worker threads
Hey Scott

Perhaps you could suggest AfxGetApp()->m_pMainWnd as a safe way to get the
main window from a secondary thread?

--

Regards,
Nish [VC++ MVP]


Quote:

> > Hello all. I've read a lot of confusing, contradictory advice regarding
> > how to post a message from a worker thread to a program's main window.

> > Joe Newcomer's opinion (from the website):
> > Never call AfxGetMainWnd() from a worker thread because a worker thread
> > has no main window and the function will return NULL. Joe then goes on
to
> > recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
> > a public place, then use it (an alternative method that I've seen Joe
use
> > in the newsgroup is to pass the CWnd* in as the thread's parameter).

> > Scott McPhillips (and www.mvps.org) opinion:
> > Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member,
and
> > call the Win32 API's PostMessage function instead of using the pointer
> > that was returned from AfxgetMainWnd and CWnd::PostMessage.

> > My question is this: Which of these opinions is correct? Also, if I
> > create my MFC project with the AppWizard, why not just access the
> > m_pMainWnd member of the global variable theApp (which is the main
thread
> > object) to get the pointer to the application's main window? Of course,
> > the question as to whether or not to use the pointer or the m_hWnd
member
> > remains.

> > Thanks,
> > Peter Huber

> Calling the MFC CWnd::PostMessage has exactly the same effect as calling
> the Win32 API ::PostMessage with the HWND.  Both ways are correct: The
> MFC version just uses m_hWnd to call the API version.

> I wrote that mvps.org FAQ answer.  It says to use the HWND for
> pedagogical reasons, and to be immune to possible changes in MFC.  The
> whole point of that FAQ is to help people who do not know that a CWnd*
> to a window in another thread will not work in many situations.  So I
> felt it was dangerous to recommend a CWnd* to such a reader.

> As for getting the HWND or CWnd* in the thread, Joe is right that
> AfxGetMainWnd will return NULL in a worker thread, and the FAQ is
> wrong.  The phrase "or you can get it with AfxGetMainWnd()->m_hWnd from
> within worker
> threads" should be deleted.  I'll see what I can do about that :-(

> --
> Scott McPhillips [VC++ MVP]



Fri, 11 Nov 2005 23:35:21 GMT  
 Posting messages to the main window from worker threads
In a regular DLL, you need to be careful as it will still return null,
unless you use AFX_MANAGE_STATE and switch to App state.

--
Ajay Kalra [MVP - VC++]


Quote:
> Hey Scott

> Perhaps you could suggest AfxGetApp()->m_pMainWnd as a safe way to get the
> main window from a secondary thread?

> --

> Regards,
> Nish [VC++ MVP]




> > > Hello all. I've read a lot of confusing, contradictory advice
regarding
> > > how to post a message from a worker thread to a program's main window.

> > > Joe Newcomer's opinion (from the website):
> > > Never call AfxGetMainWnd() from a worker thread because a worker
thread
> > > has no main window and the function will return NULL. Joe then goes on
> to
> > > recommend calling AfxGetMainWnd() from the main thread, store a CWnd*
is
> > > a public place, then use it (an alternative method that I've seen Joe
> use
> > > in the newsgroup is to pass the CWnd* in as the thread's parameter).

> > > Scott McPhillips (and www.mvps.org) opinion:
> > > Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member,
> and
> > > call the Win32 API's PostMessage function instead of using the pointer
> > > that was returned from AfxgetMainWnd and CWnd::PostMessage.

> > > My question is this: Which of these opinions is correct? Also, if I
> > > create my MFC project with the AppWizard, why not just access the
> > > m_pMainWnd member of the global variable theApp (which is the main
> thread
> > > object) to get the pointer to the application's main window? Of
course,
> > > the question as to whether or not to use the pointer or the m_hWnd
> member
> > > remains.

> > > Thanks,
> > > Peter Huber

> > Calling the MFC CWnd::PostMessage has exactly the same effect as calling
> > the Win32 API ::PostMessage with the HWND.  Both ways are correct: The
> > MFC version just uses m_hWnd to call the API version.

> > I wrote that mvps.org FAQ answer.  It says to use the HWND for
> > pedagogical reasons, and to be immune to possible changes in MFC.  The
> > whole point of that FAQ is to help people who do not know that a CWnd*
> > to a window in another thread will not work in many situations.  So I
> > felt it was dangerous to recommend a CWnd* to such a reader.

> > As for getting the HWND or CWnd* in the thread, Joe is right that
> > AfxGetMainWnd will return NULL in a worker thread, and the FAQ is
> > wrong.  The phrase "or you can get it with AfxGetMainWnd()->m_hWnd from
> > within worker
> > threads" should be deleted.  I'll see what I can do about that :-(

> > --
> > Scott McPhillips [VC++ MVP]



Sat, 12 Nov 2005 00:55:15 GMT  
 Posting messages to the main window from worker threads
In earlier versions of MFC, AfxGetMainWnd() returned NULL. This is consistent with the
documentation. However, I did the experiment, and at least from a pure worker thread, it
actually returns the app's main window. This is not consistent with the documentation, or
my past experience. I did not try the experiment from a UI thread, since I didn't have one
handy to try this with.
                                        joe

Quote:

>Hello all. I've read a lot of confusing, contradictory advice regarding
>how to post a message from a worker thread to a program's main window.

>Joe Newcomer's opinion (from the website):
>Never call AfxGetMainWnd() from a worker thread because a worker thread
>has no main window and the function will return NULL. Joe then goes on to
>recommend calling AfxGetMainWnd() from the main thread, store a CWnd* is
>a public place, then use it (an alternative method that I've seen Joe use
>in the newsgroup is to pass the CWnd* in as the thread's parameter).

>Scott McPhillips (and www.mvps.org) opinion:
>Use AfxGetMainWnd() in the worker thread, then get the m_hWnd member, and
>call the Win32 API's PostMessage function instead of using the pointer
>that was returned from AfxgetMainWnd and CWnd::PostMessage.

>My question is this: Which of these opinions is correct? Also, if I
>create my MFC project with the AppWizard, why not just access the
>m_pMainWnd member of the global variable theApp (which is the main thread
>object) to get the pointer to the application's main window? Of course,
>the question as to whether or not to use the pointer or the m_hWnd member
>remains.

>Thanks,
>Peter Huber

Joseph M. Newcomer [MVP]

Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Sat, 12 Nov 2005 03:04:43 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Posting a message from a worker thread to the main app thread

2. How to Send messages from worker thread to main thread

3. posting messages from UI thread to main app

4. posting message to main thread

5. worker thread vs main thread?

6. Handling events from a worker thread in the main thread

7. component in main threads used in worker threads

8. Synchronization problem between main thread and worker thread

9. Problems with PostThreadMessage, posting to CWinApp from worker thread

10. Posting a message to the main dialog

11. CWindowImpl, posting messages between threads

12. CWindowImpl, posting messages between threads- Urgent please

 

 
Powered by phpBB® Forum Software