Solved

Problem PostMessage in CDialog::InitDialog()

Posted on 2001-09-03
14
1,159 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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
 
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 31

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 31

Expert Comment

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

have a nice day,

regards,

ZOPPO
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 126
ODBC Connection Logging, ADO.NET 6 69
java ^ examples 8 69
MaxSpan challenge 9 105
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

839 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