debug arssertion failed

Hi,
I have a project in vc++ 6.0.
I am reading sector by sector of a volume  using Createfile() SDK function  & show informations about the process through a progressbar(CProgressCtrl) on a  dialog.
I am using SendMessage() function to send informations to the ProgressCtrl from my .cpp file to the  dialog's ProgressCtrl . But in betwen process I want to stop the process, so I just close dialog. But at that time send message is trying to send Message to the ProgresCtrl, which is not exist(becose i have already close the dialog). hence the debug assertion failed error comes.

My question is, how to solve the problem. when user press stop button I have to stop the process & close the dialog.

If I use Sleep() function before close the dialog , then it is OK, but i don't want to  use Sleep function.
pls tell me any other way to solve the problem.

what exactly happened, that
The" m_hWnd" variable is NULL.

here is the error..

_AFXWIN_INLINE BOOL CWnd::PostMessage(UINT message, WPARAM wParam, LPARAM lParam)
      { ASSERT(::IsWindow(m_hWnd)); return ::PostMessage(m_hWnd, message, wParam, lParam); }
 
davinder101Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Beofre you post the message make a check
if(IsWindow(hWnd))  PostMessage(....)
0
davinder101Author Commented:
Hi AndyAinscow,
Before Postmessage(), hWnd is a valid handle in my program.
So the above solution given by you is failed .(I have already tried before)
I mean one or more  messages(dispatched by PostMessage() function ) are already in the message queue and waiting to be process, At this time I want to close the dialog, So my dialog is closed but windows send the message(dispatched by PostMessage() and waiting for process from message queue) to the dialog.

So ask for any other way to solve the problem.
0
AndyAinscowFreelance programmer / ConsultantCommented:
OK.  As part of the closing procedure (eg in the OnDestroy) drain the message queue

      MSG msg;
      while(PeekMessage(&msg, NULL, uiMessage, uiMessage, PM_REMOVE));
where uiMessage is the message ID you want to remove
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

davinder101Author Commented:
Hi,
I have tried as per your instruction , but result is the same .
Here is my code for doing all this.

void CProgressDlg::OnDestroy()
{
      CDialog::OnDestroy();
      
      LPMSG            lpMsgOut;

      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
      
}

NOTE: We are sending  ' MSG_PROGRESSBAR_STEP ' message in PostMessage .

Thanks
0
AndyAinscowFreelance programmer / ConsultantCommented:
I've just had another look and realised the ASSERT is when you are calling postmessage.  The code in the OnDestroy is irrelevant.

Before you post the message make a check
if(IsWindow(hWnd))  
  PostMessage(....)

What you are saying is the following
if(IsWindow(hWnd))  <<---At this point hWnd is a valid window
   PostMessage(....)    <<----At this point hWnd is not a valid window

Where do you use the PostMessage?  How do you the check the window is valid?
0
davinder101Author Commented:

Hi,

I am using the PostMessage in my Thread class. i call this thread form the dialog class.

What We are saying is the following
if(IsWindow(hWnd))  <<---At this point hWnd is a valid window
   PostMessage(....)    <<----At this point hWnd is valid  but it is in the Message queue

Now i terminate the thread and close the dialog . Now ASSERT failure because Message Queue looks for the dialog handle to dispatch the message.

My question is
How to drain the message queue before closing the dialog.

Thanks
0
AndyAinscowFreelance programmer / ConsultantCommented:
I suspect the wrong question.
What you really need is to kill the thread as part of the dialog destruction, eg in the OnClose.


ps. I hope you aren't passing a CWnd pointer to another thread - MFC is NOT thread safe.  Some things will work without problems, others can fail.
0
nksaxenaCommented:
First thing, if you are going this way...

void CProgressDlg::OnDestroy()
{
      CDialog::OnDestroy();
      LPMSG   lpMsgOut;
      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
 }

you should first call PeekMessage and then CDialog::OnDestroy();
means,
void CProgressDlg::OnDestroy()
{
      LPMSG   lpMsgOut;
      while(PeekMessage(lpMsgOut, this->m_hWnd , MSG_PROGRESSBAR_STEP , MSG_PROGRESSBAR_STEP, PM_REMOVE));
    CDialog::OnDestroy();
 }

Other way is,
Check from your Thread for dialogs safe handle means call GetSafeHWnd(), like this,

If (pDlg->GetSafeHWnd())
   :: SendMessage //  (not Post)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.