Solved

Idle Processing in a dialogbox

Posted on 1997-06-04
4
381 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

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

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
Installing Microsoft R server on REDHAT or SUSE Linux 4 131
substring method in java 1 98
move a line in eclipse 3 83
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

861 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

22 Experts available now in Live!

Get 1:1 Help Now