Solved

Losing access to my dialog box in MFC

Posted on 1998-01-26
6
275 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Author Comment

by:matth012098
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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

11 Experts available now in Live!

Get 1:1 Help Now