Re: Yet another question on CProgressCtrl

Tech-Archive recommends: Fix windows errors by optimizing your registry

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)


Quantcast