Solved

Losing access to my dialog box in MFC

Posted on 1998-01-26
6
280 Views
Last Modified: 2013-12-03
I have recently been implementing an app to download some files from the internet and stumbled across the following problem:-

I bring up a dialog box using DoModal(), and in that dialog box I have a button called start. I have a handler called OnStartButtonPressed() which is called when ever I press the start button. The code within this button handler downloads a bunch of files from the internet, however I seem to not be able to interact with it once it starts downloading the files. I can no longer do any of the following:-

a) Adjust the dialogs position or size etc...
b) interact with any of the buttons or text boxes within the dialog box
c) Cancel and OK do not even respond.
d) I cant even close the dialog box.

The only way I can stop it before it has finished downloading the files is to use CTRL+ALT+DEL and shuit it down even then it hangs around in the system for a bit.

I have a CProgressCtrl in the dialog which is the only that that works.

I can get to other applications and use them fine, it seems like I have locked my dialog window up whilst the handler downloads the internet files.

How do I get around this, is it possible without using threads?

Thanx

matth
0
Comment
Question by:matth012098
  • 4
  • 2
6 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1410389
Instead of using a modal dialog, you can create a modeless dialog box. The problem is your function that downloads files is not processing any messages. You can use

  GetApp()->PumpMessages();

to process messages in your loop.

0
 
LVL 3

Author Comment

by:matth012098
ID: 1410390
PumpMessage() is a CSocket method, unfortunately I cannot use this since I am using WinINet CInternetSession (sorry, I should have mentioned this). I created a modeless dialog box as follows:-

I created a dialog box and class from it using ClassWizard
I have overriden the Create function as follows:-

CWebGet::Create(UINT nID, CWnd *pWnd)
{
  return CDialog::Create(nID, pWnd);
}
I also overriden OnOK(), OnCancel() and CWnd::PostNcDestroy() in the dialog class as follows:-

void CWebGet::OnCancel()
{
    DestroyWindow();
}
void CWebGet::OnOK()
{
    if (!UpdateData(TRUE))
    {
       return;
    }
    DestroyWindow();
}
virtual void CWebGet::PostNcDestroy() {delete this;}

I then create the dialog box like this:-

CWebGet *dlg = new CWebGet;
dlg.Create(IDD_DIALOG_WEB, this);

The dialog box is displayed and I can interact with it, but as soon as I click the START button and the handler starts downloading internet files, I am locked out of the dialog box again, I have still the same probs.

matth

0
 
LVL 3

Author Comment

by:matth012098
ID: 1410391
Also, the dialog box is not getting redrawn whilst downloading. If I switch to a dos prompt and alt-TAB back to my application the window is white with borders. I guess this is something to do with the dialog box not processing the redraw message. I understand that creating a modeless dialog is ran as a seperate thread to the window that created it, and when I press START on the dialog, it gets trapped in my loop downloading files from the net, so the dialog window will not process messages. Is there a specific command to force the dialog to handle any messages within the internet download loop?


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:matth012098
ID: 1410392
I have found half the solution to this question and that is to use the win32 functions GetMessage(), TranslateMessage() and DispatchMessage(). I wrote the following function to take care of it

BOOL ProcessMessages(HWND hWnd, int loop)
{
  MSG msg;
  int t;
  BOOL ret = TRUE;

  for (t=0; t<loop; t++)
  {
    ret=GetMessage(&msg, hWnd, 0, 0);
    TranslateMessage(&msg);
    DispatchMessage(&mdg);
    if (!ret)
      break;
  }
  return ret;
}

This will process a number of messages for the required window which is in my case the dialog box defined by CWebGet.

This works but it maybe 2 seconds or so before the message is acknowledged, if anyone knows of a better way especially in MFC of doing this that I can easily implement I will award them the points

matth

0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 70 total points
ID: 1410393
Sorry, added an extra s at the end of PumpMessage() which is a virtual function of CWinThread. CWinApp is derived from CWinThread.
0
 
LVL 3

Author Comment

by:matth012098
ID: 1410394
Thanks

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

15 Experts available now in Live!

Get 1:1 Help Now