Solved

Problem PostMessage in CDialog::InitDialog()

Posted on 2001-09-03
14
1,129 Views
Last Modified: 2013-11-20
In a dialog based application I have two dialogs which I call form CMyApp::InitInstance().

Like:

CFirstDlg dlg1;
dlg1.DoModal();

CSecondDlg dlg2;
dlg2.DoModal();

To start an automatic process (like searching files on a hard disk)I post a user message from within CFirstDlg::InitDialog()

When I run the release build .exe I get an Assertion Failure within MFC-code. Though, when building and running the debug .exe it all works fine!

I already know that the problem is the PostMessage cause when I remove it, no assertion failure occures.

I guess a solution could be to build a modeless dialog but are there other solutions which will keep using DoModal?

Eelko.
0
Comment
Question by:eelko
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6450385
Post a message to what window ? Be more specific.
0
 
LVL 2

Author Comment

by:eelko
ID: 6450402
Post a message to myself, e.g.:

BEGIN_MESSAGE_MAP(CFileSearcherDlg, CDialog)
 ON_MESSAGE(UM_STARTSEARCH, OnStartSearch)
END_MESSAGE_MAP()

BOOL CFirstDlg::InitDialog()
{
 CDialog::OnInitDialog();

 PostMessage( UM_STARTSEARCH );
 return TRUE;  // return TRUE unless you set the focus to a control
              // EXCEPTION: OCX Property Pages should return FALSE
}
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6450446
Try this code on your init dialog:

ShowWindow( SW_SHOW);
RedrawWindow();
OnStartSearch();
0
 
LVL 2

Author Comment

by:eelko
ID: 6450495
No change, though, I now see the window before the error appears.
In OnStartSearch I post again a message (to myself) and probably the program now fails during this post.

I found out that the problem lies within CWnd::GetDlgItem() (in winocc.cpp). In debug probably m_pCtrlCont == NULL since all data in debug mode is initialized to 0.
In release mode m_pCtrlCont is invalid and m_pCtrlCont->GetDlgItem(nID, phWnd); fails.

void CWnd::GetDlgItem(int nID, HWND* phWnd) const
{
     ASSERT(::IsWindow(m_hWnd));
     ASSERT(phWnd != NULL);

     if (m_pCtrlCont == NULL)
          *phWnd = ::GetDlgItem(m_hWnd, nID);
     else
          m_pCtrlCont->GetDlgItem(nID, phWnd);
}
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6450511
Can you post the whole code for CFirstDlg ? I want to debug it myself.

Why are u trying to post messages from within the dialog when u can invoke methods ?
0
 
LVL 2

Author Comment

by:eelko
ID: 6450561
The FirstDlg.cpp file:


#define UM_STARTSEARCH     WM_APP + 1
#define UM_SEARCHNEXT     WM_APP + 2
#define UM_SEARCHDONE     WM_APP + 3

/////////////////////////////////////////////////////////////////////////////
// CFirstDlg dialog

CFirstDlg::CFirstDlg(CWnd* pParent /*=NULL*/)
     : CDialog(CFirstDlg::IDD, pParent)
{
     //{{AFX_DATA_INIT(CFirstDlg)
          // NOTE: the ClassWizard will add member initialization here
     //}}AFX_DATA_INIT
     m_pSearcher = NULL;
}

void CFirstDlg::DoDataExchange(CDataExchange* pDX)
{
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CFirstDlg)
          // NOTE: the ClassWizard will add DDX and DDV calls here
     //}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CFirstDlg, CDialog)
     //{{AFX_MSG_MAP(CFirstDlg)
     //}}AFX_MSG_MAP
     ON_MESSAGE(UM_STARTSEARCH, OnStartSearch)
     ON_MESSAGE(UM_SEARCHNEXT, OnSearchNext)
     ON_MESSAGE(UM_SEARCHDONE, OnSearchDone)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFirstDlg message handlers

BOOL CFirstDlg::OnInitDialog()
{
     CDialog::OnInitDialog();
     
     m_pSearcher = new CSearcher(this);
     PostMessage(UM_STARTSEARCH);
     
     return TRUE;
}

void CFirstDlg::OnCancel()
{
     if( m_pSearcher )
          m_pSearcher->m_bStopSearch = TRUE;
     PostMessage(UM_SEARCHDONE, FALSE);
}

LRESULT CFirstDlg::OnStartSearch()
{
     m_pSearcher-> SetFileToSearch("Notepad.exe");
     PostMessage(UM_SEARCHNEXT);
     return 0;
}

LRESULT CFirstDlg::OnSearchNext()
{
     if( m_pSearcher-> Search() )
          PostMessage(UM_SEARCHDONE, TRUE);
     return 0;
}

LRESULT CFirstDlg::OnSearchDone(BOOL bResult)
{
     delete m_pSearcher;

     if( bResult )
          CDialog::OnOK();
     else
          CDialog::OnCancel();
     return 0;
}

The CSearcher object does the real work. It scans all drives and its directories for the specified file(s). The object builds up a list with directories to search (initially only the root directory of each drive). The Search method will search the first directory of this list, add names of the subdirectories to the list and sends the UM_SEARCHNEXT message to the dialog. It is done when the list is empty.

The reason why I use so much posting of messages is that if I don't the user will not be able to press the "Cancel"-button since this event is handled after the method is completed and the dialog gets back to its message loop.

But if you know a better way to do this, I will be happy to hear it.
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6450649
First for a window u can use WM_USER + X message.

Second, the search operation is done in OnSearchNext, the OnSearchDone and OnStartSearch are unsignifiant form the point of execution time so the app will hang until the OnSearchNext execution is completed. So that PostMessage thing isn't helping at all.

The solution will be to put the search process in a separate thread, a working thread that will do all the work in background. Look at AfxBeginThread function ( first form )to learn how to work with working threads. The AfxBeginThread returns a pointer to a CWinThread class that u can use it to stop the search when user press the cancel. U can pass to the thread proc a pointer to your window to update a progress bar or somenthing else to show the progress of search to the user ( if u want).

A little example:

UINT thread_proc( LPVOID pParam )
{
    m_pSearcher-> SetFileToSearch("Notepad.exe");
    if ( m_pSearcher-> Search() )
         (CFirstDlg*)pParam->OnOk();
    else
         (CFirstDlg*)pParam->OnCancel();
}

BOOL CFirstDlg::OnInitDialog()
{
    CDialog::OnInitDialog();
   
    AfxBeginThread( thread_proc, this );
    return TRUE;
}
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:eelko
ID: 6450965
I modified the code like in your sample but now the searcher object does nothing and form the Dialog object I can't access the searcher object to stop the search. Somewhere in MSDN I read that I cannot use MFC objects in two different threads. It seems to me that this is a lot of extra work for something that already works in debug mode.
0
 

Expert Comment

by:rsraj
ID: 6450997
Hi,

We face no problems in debug mode exe but do face assertion failure in release mode exe.
Comming to the problem,

To get a solution without using modeless dlg box shall be,

Declare application document template and initialize it.
Then declare the document pointer and initialise it using the document template pointer member function.
Once the document pointer is obtained, it is easy to obtain its view.

Then call PostMessage function on the view window.

regards
sun


0
 
LVL 30

Accepted Solution

by:
Zoppo earned 75 total points
ID: 6451012
Hi eelko,

about original problem 'When I run the release build .exe I get an Assertion Failure
within MFC-code. Though, when building and running the debug .exe it all works fine!'

I think this behavior comes from a wrong defined message handler function:
Take a look at MSDN article 'User-Defined Handlers'
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_user.2d.defined_handlers.asp
There it's stated that functions used as message handler with ON_MESSAGE need
to be declared as afx_msg LRESULT memberFxn(WPARAM, LPARAM); but you
decalre them as afx_msg LRESULT memberFxn();

MFC somehow catches that in DEBUG but not in RELEASE.

hope that helps,

ZOPPO
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6451048
In the code that I post it's intended to work with the dialog using the pointer passed in pParam. The search object need to exist only in the thread. To stop the thread use a CWinThread * pWinThread = AfxBeginThread( thread_proc, this ) object and use the CWinThread::SuspendThread and CWinThread::ResumeThread to pause and resume the search. Use a var in your dialg to pass the search object the file name. You can also use a var in your dialog( that the thread_proc checks periodicaly ) to cancel the execution of the thread.

Good luck.
0
 
LVL 2

Expert Comment

by:BogdyPtr
ID: 6451073
What kind of object is CSearcher ?
0
 
LVL 2

Author Comment

by:eelko
ID: 6451117
Excellent! I fixed it and now it works perfect.
Thanks, Eelko.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 6451154
No problem ... you're welcome.

have a nice day,

regards,

ZOPPO
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now