Re: Yet another question on CProgressCtrl

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alvaro Palma (no_spam_at_no_spam.com)
Date: 01/12/05


Date: Wed, 12 Jan 2005 18:18:37 -0300

KK wrote:
> Greetings,
> I'm a newbie to MFC, I could successfully implement the CProgressCtrl
> through controls box available with the .rc file(the resource editor).
> So in all I have 3 objects now, "progress bar", "Ok" & "Cancel". When
> the user presses OK button, the bar progresses. However, I want to
> incorporate another feature where on pressing "Cancel" button, the
> execution should stop. Could anyone please let me know how to go about
> doing this :( ? The following is the code.
> void CMyAPPDlg::OnOK(){
> m_Progress.SetRange(0,100);
> m_Progress.SetStep(2);
> for(int i=0;i<50;i++)
> {
> SomeFunction(i);
> m_Progress.StepIt();
> }
> }
>
> void CMyAPPDlg::OnCancel()
> {
> exit(1);
> }
>
> Pressing the cancel button, does not exit the program. Please help me
> out with this problem.

What you have to do is, when the user presses OK, start a new thread of
execution, which can be stopped using Cancel. Something like (in pseudo
code)

CMyAPPDlg::OnOK(){
        SetFlagForThread();
        AfxBeginThread(threadFunction);
}

CMyAPPDlg::OnCancel(){
        UnsetFlagForThread();
}

CMyAppDlg::threadFunction(data){
        While(flag_for_thread){
                ModifyProgressBar();
        }
}

Read in the MSDN about AfxBeginThread, and how to declare a thread
function. It's relatively simple.

PS: BTW, and just for clarity, since the Accept and Cancel buttons are
starting and stopping a process, IŽll sugest that you rename the
function associated to them as OnStart and OnStop, to diferentiate them
from CDialog::OnOK and CDialog::OnCancel (cause you're not overriding
the base function)



Relevant Pages

  • Re: Yet another question on CProgressCtrl
    ... editor). ... >> the user presses OK button, ... >> Pressing the cancel button, ... > execution, which can be stopped using Cancel. ...
    (microsoft.public.vc.mfc)
  • Executors, Futures, and Cancelation
    ... So far, I have gotten a lot of use out of the API, and used it on a ... a method to cancel the task, ... for execution. ... call myCleanup() for me, or if it is still "in queue," and will never ...
    (comp.lang.java.programmer)
  • Re: Validation On An InputBox
    ... presses 'Cancel' the command is cancelled (as I would ... >> Can I add validation to an input box? ... >> user presses 'Cancel' to simply do nothing and return to ... regardless of whether the user presses 'OK' ...
    (microsoft.public.access.formscoding)
  • Re: DoCmd.Close command does not terminate form code execution
    ... executing a Close does not immediately terminate the procedure that is running. ... END OF FILE loop. ... true records the cancel event to an audit log, ... code execution stopped, but this is apprently not the case here. ...
    (microsoft.public.access.modulesdaovba)
  • [PATCH 4/4] workqueue: implement flush[_delayed]_work_sync()
    ... earlier queueing instances. ... * Wait until @work has finished execution. ... the delayed work to flush ... @dwork: the delayed work cancel ...
    (Linux-Kernel)