Re: UIThreads, dialogs & messaging problem
Simon L wrote:
In eVC4 .... I've built a CWinthread derived class to display a logon
dialog non-modally and it communicates with the main app thread via
messaging. So every 30 seconds or so, if the user hasn't been doing
anything, a dialog pops up on top of everything else requiring a logon.
(Comments on the validity of this mechanism welcome).
I agree with David Wilkinson's comments that this is a poor design in
several ways. As a non-modal dialog it also will not have your
apparently intended effect of blocking the main GUI.
Calls to show the dialog are made in the Main app as
pThread->PostThreadMessage( WM_SHOWME .... )
and handled in the thread
ON_MESSAGE( WM_SHOWME , OnShow ) [changed from ON_THREAD_MESSAGE
after googling this group about a problem that was fixed in VC7 - not
sure how this applies to eVC]
afx_msg LRESULT MyThread::OnShow (WPARAM, LPARAM)
{
Create if necessary, and show a dialog
}
This is a mistake. You must not use PostThreadMessage to a thread that
displays any GUI. It is documented to fail in the PostThreadMessage
docs. In order to make it work your thread must create an HWND and the
main thread can safely PostMessage to the HWND.
When the logon dialog closes (hides), it posts a message to the main
app (via AfxGetApp()->m_pMainWnd->PostMessage(WM_CREW ) , 45 , 18 )
and handled in a CView derived class as
ON_MESSAGE( WM_CREW , OnLogon )
afx_msg LRESULT MyView::OnLogon(WPARAM wParam , LPARAM lParam)
This is a mistake. If you want to handle the message in the view then
the thread must post it to the view. And, define your messages this way:
#define MY_WM_SHOWME (WM_APP)
#define MY_WM_CREW (WM_APP+1)
--
Scott McPhillips [VC++ MVP]