Solved

display a lengthy operation thru' progress bar

Posted on 2000-04-26
6
756 Views
Last Modified: 2013-11-20
I am display a modal dialog box to see the progress time for a lengthy method... This is done through a progress bar ... It is an SDI application in VC5.. I call the progress control in OnInitDialog() ...
All of my code is written in the ..View.cpp file (the calculation) ...
This is how the whole operation should work ... The user hits a start button on menu which causes a dialog box to pop up ... only he OKAY;s it another dialog box will pop up ... When the user hits the OK button on this dialog the progress bar should display the progress of the calculation in the progress bar ... Since my counter (Counter) is changing inside the Contingency function I am calling the setpos(..)inside it .... that will show the counter is changing and with that the progress bar will move ... IT GIVES ME AN ASSERTION ERROR AND GOES TO

_AFXCMN_INLINE CProgressCtrl::CProgressCtrl()
      { }
_AFXCMN_INLINE void CProgressCtrl::SetRange(int nLower, int nUpper)
      { ASSERT(::IsWindow(m_hWnd)); ::SendMessage(m_hWnd, PBM_SETRANGE, 0, MAKELPARAM(nLower, nUpper)); }
_AFXCMN_INLINE int CProgressCtrl::SetPos(int nPos)
      { ASSERT(::IsWindow(m_hWnd)); return (int) LOWORD(::SendMessage(m_hWnd, PBM_SETPOS, nPos, 0L)); }
_AFXCMN_INLINE int CProgressCtrl::OffsetPos(int nPos)
      { ASSERT(::IsWindow(m_hWnd)); return (int) LOWORD(::SendMessage(m_hWnd, PBM_DELTAPOS, nPos, 0L)); }
_AFXCMN_INLINE int CProgressCtrl::SetStep(int nStep)
      { ASSERT(::IsWindow(m_hWnd)); return (int) LOWORD(::SendMessage(m_hWnd, PBM_SETSTEP, nStep, 0L)); }
_AFXCMN_INLINE int CProgressCtrl::StepIt()
      { ASSERT(::IsWindow(m_hWnd)); return (int) LOWORD(::SendMessage(m_hWnd, PBM_STEPIT, 0, 0L)); }



points at SetPos() .. I am attaching the code ...
Though this was simple ... All application I have seen in Kruglinski's book writes the code in the Dialog.cpp on ONOK (derived from the class wizard) and the lengthy calculation is performed there


Thanks in advance



BOOL Cdiacalprogress::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here
//     CProgressCtrl * pBar = (CProgressCtrl *) GetDlgItem(IDC_PROGRESS1);
     // m_nprogress is the control variable generated from class wizard
     m_nprogress.SetRange(0, 100);
     m_nprogress.SetPos(Counter); // counter=0
      
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}

void CSMIBView::OnStartprocess()
{
.....
....
                Cdiacalprogress dlgprog;
//                    dlgprog.DoModal();
                        if (dlgprog.DoModal() == IDOK)
                        {

      // These are three conditions that needs initialization
                        pline_array3 = make_line_array3(m_NoPoints); // call the function
      // **** Three types of contingencies will be investigated here ****
                        // *** Pre-fault  During-fault  Post-fault ****
                        Counter = 0; // this is the counter that will get incremented inside the Contingency function
                        Time_Counter = tstart + (Counter*tstep);
...
...
// THIS IS THE FUNCTION THAT WILL BE CALLED THREE TIMES AND THE COUNTER KEEPS TICKING STARTING FROM 0 TO 1000

                        Contingency(tstart,tfault,prevolt,prexth); // function call
                        //   outfile<<"\n";
                        //   outfile<<"\n";
//                         Contingency(tfault,tclear,durvolt,durxth); // function call

                        Contingency(tclear,tstop,postvolt,postxth); // function call
} // dlgprogress ends here

} // OnStartprocess function ends here
...
...
...
void Contingency(double t_begin,double t_end,double VTH, double XTH)
{

...
counter++;
 CProgressCtrl m_nprogress;
    m_nprogress.SetPos(Counter * 100 / m_NoPoints);
....
}
0
Comment
Question by:arindam042099
  • 3
  • 3
6 Comments
 

Author Comment

by:arindam042099
Comment Utility
I am derived the max time from
    m_NoPoints = (int)((tstop-tstart)/tstep); // MaxCount
where if tstep=0.001,tstop=1sec,tstart=0,m_NoPoints=1000

Besides I also have the following code written
void Cdiacalprogress::OnCancel()
{
      // TODO: Add extra cleanup here
    TRACE("entering CComputeDlg::OnCancel\n");
    if (Counter == 0) {      // prior to Start button
        CDialog::OnCancel();
    }
    else {                    // computation in progress
        Counter = m_NoPoints; // Force exit from OnStart
    }
}
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 10 total points
Comment Utility
You are not referring to the progress control in the dialog (which has disappeared by now anyway).

Instead, you've created a local progress control C++ object in your function (that isn't attached to a real progress control) and tried to do things to it (which fail, because there is not corresponding progress control window).
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Either make the dialog modeless, and get a pointer/reference to the progress control in that dialog.  Or put the Contingency function inside the dialog class itself.

You may want to use a progress dialog in a separate thread (there is several examples of code around for this).  This gives better response (otherwise the message loop doesn't process events properly while you length operation is running.  Other alternatives is to put the body of your processing loop in a message handler, and call SendMessage in a loop for each iteration.

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:arindam042099
Comment Utility
I had tried to use the pointer concept before but if I even use that
pointer->SetPos(count*100/maxpoints)
it still gave me an assertion error

when I did this it worked (code below)
void Cdiacalprogress::OnCancel()
{
      // TODO: Add extra cleanup here
    TRACE("entering CComputeDlg::OnCancel\n");
    if (m_nCount == 0) {      // prior to Start button
        CDialog::OnCancel();
    }
    else {                    // computation in progress
        m_nCount = m_NoPoints; // Force exit from OnStart
//    m_nprogress.SetPos(0);

    }
}


BOOL Cdiacalprogress::OnInitDialog()
{
      CDialog::OnInitDialog();
      
      // TODO: Add extra initialization here

      
      return TRUE;  // return TRUE unless you set the focus to a control
                    // EXCEPTION: OCX Property Pages should return FALSE
}
// **********************************************************************

void Cdiacalprogress::OnOK()
{
      // TODO: Add extra validation here
   MSG message;

     CProgressCtrl * pProgress = (CProgressCtrl *) GetDlgItem(IDC_PROGRESS1);

     pProgress->SetRange(0, 100);
     pProgress->SetPos(0); // counter=0
   GetDlgItem(IDOK)->EnableWindow(FALSE);


    for ( m_nCount = 0; m_nCount <= m_NoPoints;m_nCount++)
    {
    pProgress->SetPos(m_nCount*100/m_NoPoints);
    }
//    m_nprogress.SetPos(0);
      
        if (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
            ::TranslateMessage(&message);
            ::DispatchMessage(&message);
        }

      CDialog::OnOK();
}
// *************************************************************

But see I do not want that, I want the counter value to come from the Contingency function and it will be too much of a hassle in terms of externs to put the Contingency function inside the dialog class
what did you mean by this ???
You are not referring to the progress control in the dialog (which has disappeared by now anyway).

Instead, you've created a local progress control C++ object in your function (that isn't attached to a real progress control) and tried to do things to it (which fail, because there is not corresponding progress control window).


WITH MY LIMITED KNOWLEDGE i think when I call the CProgressCtrl class the progress bar controls are initiated ...
Instead of using a pointer to that progress class I used objects ... what is wrong in that ???

I know how to incorporate a modeless dialog but how will that solve the problem ....


>>>>>>>You may want to use a progress >dialog in a separate thread (there is >several examples of code around for >this).  This gives better response >(otherwise the message loop doesn't >process events properly while you >length operation is running.  Other >alternatives is to put the body of >your processing loop in a message >handler, and call SendMessage in a >loop for each iteration.

You lost me completely here ... were do I find such examples ... I use Kruglinki's book for learning ... Please note:This is not a project it is part of my learning with VC ...

Thank you again for all the help
looking forward to hear from you
0
 
LVL 10

Expert Comment

by:RONSLOW
Comment Utility
Have a look at the MSJ "Wicked Code" column February 1997

This shows how to implement a dialog with a progrtess bar that runs while you do some lengthy process .. just what you want to do.

You can doens'lot the source code 9and read the article) from the MSJ site.

0
 

Author Comment

by:arindam042099
Comment Utility
Thanks
That was what I needed
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 66
Hibernate methods 2 58
zeroFront challenge 7 70
Detect file exist or not 3 74
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…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

6 Experts available now in Live!

Get 1:1 Help Now