Solved

Idle Processing in a dialogbox

Posted on 1997-06-04
4
380 Views
Last Modified: 2013-11-20
Hello,

I am developing a Windows NT application which has only one dialog box.  It does lot of background processing and provides a feedback to user through the dialog box.  There is no interaction required from the user.  As soon as the application is started, it brings up this dialog box and starts processing.  So I created a dialog based application usin MFC, but once the "DoModal()" function is called for this dialog box class, then how do I do the idle processing after that??
0
Comment
Question by:karim
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
The Master earned 50 total points
ID: 1301977
There are two ways of doing this:

1) If your dialog procedure is doing the actual "background processing" you can just provide visual feedback during the processing.  Assuming you were doing something like copying a file and displaying how much has been copied, every time you copied 5% of the file's total size, you could update the display.

2)  At the end of your OnInitDialog() function, you could post a message to yourself with PostMessage(), and then start a PeekMessage() loop.

In your header file...

#define WM_STARTMSGLOOP        WM_USER + 501
...
afx_msg LONG StartMsgLoop (UINT wParam, LONG lParam);

Then in your cpp file...

BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
...
ON_MESSAGE (WM_STARTMSGLOOP, StartMsgLoop)
...
END_MESSAGE_MAP()

...

CMyDialog::OnInitDialog()
{
...
    PostMessage (WM_STARTMSGLOOP);
}

CMyDialog::StartMsgLoop (UINT wParam, LONG lParam)
{
    MSG msg;
    BOOL bDoneProcessing = FALSE;
    while (!bDoneProcessing)
    {
        if (PeekMessage (&msg, GetSafeHwnd(), 0, 0, PM_REMOVE)
        {
            /* if there's a cancel button you could check it here */
            if (msg.message == WM_COMMAND &&
                LOWORD (wParam) == IDCANCEL)
            {
                break;
            }

            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
        else
        {
            /* call idle function or do idle processing here */
            /* set bDoneProcessing to TRUE when finished */
        }
    }

    EndDialog (IDOK);
}
0
 
LVL 2

Expert Comment

by:The Master
ID: 1301978
Sorry about the indentation in the above answer, the editor took out the extra spacing.
0
 

Author Comment

by:karim
ID: 1301979
Thank you very much for providing a reply.  I tried the second method u proposed and I got following two errors at compile time:

error C2556: 'StartProcessing' : overloaded functions only differ by return type

error C2371: 'StartProcessing' : redefinition; different basic types


Now I don't know why would it think that I am trying to over load this function.  The parent class "CDialog" does not have any function called "StartProcessing".  Following is the listing of CSettlementDlg class definition from the .h file.  I opologize for the indentation in advance:

// SettlementDlg.h : header file
//

// define a user defined msg
#define WM_STARTPROCESSING WM_USER + 501

/////////////////////////////////////////////////////////////////////////////
// CSettlementDlg dialog

class CSettlementDlg : public CDialog
{
// Construction
public:
      CSettlementDlg(CWnd* pParent = NULL);      // standard constructor

// Dialog Data
      //{{AFX_DATA(CSettlementDlg)
      enum { IDD = IDD_SETTLEMENT_DIALOG };
            // NOTE: the ClassWizard will add data members here
      //}}AFX_DATA

      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CSettlementDlg)
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);      // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      HICON m_hIcon;

      // Generated message map functions
      //{{AFX_MSG(CSettlementDlg)
      virtual BOOL OnInitDialog();
      afx_msg void OnPaint();
      afx_msg HCURSOR OnQueryDragIcon();
      afx_msg LONG StartProcessing (UINT wParam, LONG lParam);
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};


Well and also in my .cpp file I have:
               .
               .
               .

BEGIN_MESSAGE_MAP(CSettlementDlg, CDialog)
      //{{AFX_MSG_MAP(CSettlementDlg)
      ON_WM_PAINT()
      ON_WM_QUERYDRAGICON()
      ON_MESSAGE (WM_STARTPROCESSING, StartProcessing)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

               .
               .
               .

CSettlementDlg::StartProcessing (UINT wParam, LONG lParam)
{
      
      return 1L;
}


So if you can spot a problem in this please let me know.  Thanks very much for your help
0
 
LVL 2

Expert Comment

by:The Master
ID: 1301980
Yes, that's a common error - I get it all the time when I change the return type of a function in either the .H or .CPP file, but not in the other.

What's happening in your case is that you have StartProcessing() defined as returning a LONG in your header file, but you don't have a return type in your .CPP file.  I'm not sure if it assumes you want to return int or void, but it will assume one of those, and so it thinks the function it's compiling is an overloaded function (same name but different parameters or return types).  By the rules of C++, you cannot have two functions with the same name, that vary only in their return type.  That's why you're getting those errors.

Your function should look like this:

LONG CSettlementDlg::StartProcessing (UINT wParam, LONG lParam)
{

return 1L;
}

Hope that helps!

Bill.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

747 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

9 Experts available now in Live!

Get 1:1 Help Now