• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1231
  • Last Modified:

Problem PostMessage in CDialog::InitDialog()

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
eelko
Asked:
eelko
  • 6
  • 5
  • 2
  • +1
1 Solution
 
BogdyPtrCommented:
Post a message to what window ? Be more specific.
0
 
eelkoAuthor Commented:
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
 
BogdyPtrCommented:
Try this code on your init dialog:

ShowWindow( SW_SHOW);
RedrawWindow();
OnStartSearch();
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
eelkoAuthor Commented:
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
 
BogdyPtrCommented:
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
 
eelkoAuthor Commented:
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
 
BogdyPtrCommented:
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
 
eelkoAuthor Commented:
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
 
rsrajCommented:
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
 
ZoppoCommented:
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
 
BogdyPtrCommented:
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
 
BogdyPtrCommented:
What kind of object is CSearcher ?
0
 
eelkoAuthor Commented:
Excellent! I fixed it and now it works perfect.
Thanks, Eelko.
0
 
ZoppoCommented:
No problem ... you're welcome.

have a nice day,

regards,

ZOPPO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 6
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now