How can I use a button to break out of a while loop? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How can I use a button to break out of a while loop?

paulhaynespaulhaynes Posts: 5Member
[b][red]This message was edited by paulhaynes at 2007-3-25 19:18:31[/red][/b][hr]
*** MESSAGE HAS BEEN REPOSTED IN THE WINDOWS PROGRAMMING SECTION ***
This seems like a simple thing to accomplish but I cannot get it figured out.

I'm running a while loop that is started by a button's OnClick event. I want to use another button's OnClick event to break out of the while loop started by the first button.

For example:

Form1::Button1OnClick(){
while(bContinueLooping){
//Code goes here
}
}

Form1::Button2OnClick(){
bContinueLooping = false;
}

Desired results:
Clicking Button1 causes the loop to start.
Clicking Button2 causes the loop to end.

The problem is that as long as the while loop is running, Button2 doesn't get recognized.

Anyone know how to break out of a while loop using this "press a button to exit" method or have a suggestion for a better way to handle it?

This is a 32-bit Windows program.





Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : This seems like a simple thing to accomplish but I cannot get it figured out.
    :
    : I'm running a while loop that is started by a button's OnClick event. I want to use another button's OnClick event to break out of the while loop started by the first button.
    :
    : For example:
    :
    : Form1::Button1OnClick(){
    : while(bContinueLooping){
    : //Code goes here
    : }
    : }
    :
    : Form1::Button2OnClick(){
    : bContinueLooping = false;
    : }
    :
    : Desired results:
    : Clicking Button1 causes the loop to start.
    : Clicking Button2 causes the loop to end.
    :
    : The problem is that as long as the while loop is running, Button2 doesn't get recognized.
    :
    : Anyone know how to break out of a while loop using this "press a button to exit" method or have a suggestion for a better way to handle it?
    :
    : This is a 32-bit Windows program.
    :
    :

    The problem is that your loop is taking up all processing power.
    To be honest, I really don't know enough about C++ or MFC to help you with this. Perhaps you can find a solution with what little I can offer yourself :) Or otherwise, let's hope one of the guru's responds ^^


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry

  • bilderbikkelbilderbikkel Posts: 754Member
    : : Form1::Button1OnClick(){
    : : while(bContinueLooping){
    : : //Code goes here
    : : }
    : : }
    : :
    : : Form1::Button2OnClick(){
    : : bContinueLooping = false;
    : : }

    This looks pretty much to C++ Builder code to me, why didn't you put it there (http://www.programmersheaven.com/c/MsgBoard/wwwboard.asp?Board=1044&src=13) ?

    What you want to add is the following line:

    [code]
    Application->ProcessMessages();
    [/code]

    Now all new OnClick Events will be processed.

    See ya,
    bilderbikkel

  • paulhaynespaulhaynes Posts: 5Member
    : : : Form1::Button1OnClick(){
    : : : while(bContinueLooping){
    : : : //Code goes here
    Application->ProcessMessages();
    : : : }
    : : : }

    :
    : What you want to add is the following line:
    :
    : [code]
    : Application->ProcessMessages();
    : [/code]
    :
    : Now all new OnClick Events will be processed.
    :
    : See ya,
    : bilderbikkel
    :
    :
    Added the suggested line as indicated in the sample code above and it works like a charm. Thanks much. Onward to the next hurdle :)


Sign In or Register to comment.