Losing access to my dialog box in MFC

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?


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.

Tommy HuiEngineerCommented:
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


to process messages in your loop.

matth012098Author Commented:
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()
void CWebGet::OnOK()
    if (!UpdateData(TRUE))
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.


matth012098Author Commented:
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?

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

matth012098Author Commented:
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);
    if (!ret)
  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


Tommy HuiEngineerCommented:
Sorry, added an extra s at the end of PumpMessage() which is a virtual function of CWinThread. CWinApp is derived from CWinThread.

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
matth012098Author Commented:

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
Microsoft Development

From novice to tech pro — start learning today.