Idle Processing in a dialogbox

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??
karimAsked:
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.

The MasterCommented:
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

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
The MasterCommented:
Sorry about the indentation in the above answer, the editor took out the extra spacing.
0
karimAuthor Commented:
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
The MasterCommented:
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
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
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.