Solved

Losing access to my dialog box in MFC

Posted on 1998-01-26
6
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and 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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 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