By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,584 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

breaking out of a while loop by clicking on a button

P: 3
Hi, my problem is apparently simple: I have a while loop controlled by a flag and I want to interrupt it by clicking on a stop button.
Expand|Select|Wrap|Line Numbers
  1. void buttonStart_Click( Object * sender, System::EventArgs * e)
  2. {
  3.       while myFlag=true;
  4.     {
  5.     // the process starts and the loop goes on
  6.    //   ...........
  7.     }
  8. }
  9.  
  10. void buttonStop_Click( Object * sender, System::EventArgs * e)
  11. {
  12. // this should stop the loop
  13.    myFlag=false;
  14. }
  15.  
But clearly even if I click on stop the system is busy running the loop and doesn't see the flag has been changed. Is there a way to do this thing without going multithread? I am using VC++ on framework 1.1. Thanks
Oct 26 '07 #1
Share this Question
Share on Google+
9 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Did you consider returning a boolean (instead of your void)?
Oct 26 '07 #2

P: 3
Thank you for your quick reply, Sicarie, but could you please explain in more detail? I am very new in c++.
Oct 26 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Expand|Select|Wrap|Line Numbers
  1.  
  2. void buttonStop_Click( Object * sender, System::EventArgs * e)
  3. {
  4. // this should stop the loop
  5.    myFlag=false;
  6. }
  7.  
You return nothing here (as you should with a 'void' declaration). Change your function declaration to return a boolean value, and then add a return statement to the definition so that you return the value in myFlag.
Oct 26 '07 #4

Expert 100+
P: 464
That wouldn't work. The function will never return until the while loop gets completed. The while loop won't complete until you click on the button. The window will not process the second button click because your in the while loop. I'm not sure how/if you can do what you want to do.
Oct 26 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
That wouldn't work. The function will never return until the while loop gets completed. The while loop won't complete until you click on the button. The window will not process the second button click because your in the while loop. I'm not sure how/if you can do what you want to do.
I've given up trying to do whole code revisions, I do one step at a time. I was also going to suggest changing the condition of the while loop, though you are right, it seems this would just post an exit message and freeze the program until the user clicked okay (which some programs - Word, etc...) do.

nunzio - does that sound like what you are trying to do? Or are you trying to let the user exit whenever they want?
Oct 26 '07 #6

Expert 100+
P: 464
What do you want to do in the while loop. What do you continually do until the user clicks the button?
Oct 26 '07 #7

P: 90
You would need to create a thread and run the loop within that thread so the form continues to process windows events (i.e. button click)

Both threads (the main thread and the loop´s thread) would then check for the flag value, the flag variable should be protected to avoid concurrent access.
Oct 26 '07 #8

P: 3
the process within the while loop is an acquisition via serial port. the application checks the value of a variable (the speed of a motor) and stores it in an array for further analysis. I just need to stop the communication with a stop button. The thing suggested by sicarie actually doesn't work as the software never enters the stopbutton_click function. Suggestions are welcome. tks nunzio
Oct 29 '07 #9

P: 90
hello? yooohooo
you need to use threads dude
:
Expand|Select|Wrap|Line Numbers
  1. void buttonStart_Click( Object * sender, System::EventArgs * e)
  2. {
  3.     //<---- SPAWN A THREAD HERE SO THE MAIN THREAD CAN CONTINUE PROCESSING INPUT FROM THE USER
  4. }
  5.  
  6. void threadRoutine( ...)
  7. {
  8.  
  9.       while myFlag=true;//<--- CHECK THE VALUE OF FLAG USING SOME MULTITHREADING SECURE FUNCTION, I.E, USE A CRITICAL SECTION
  10.     {
  11.     // the process starts and the loop goes on
  12.    //   ...........
  13.     }
  14.  
  15. }
  16.  
  17. void buttonStop_Click( Object * sender, System::EventArgs * e)
  18. {
  19. // this should stop the loop
  20.    myFlag=false; // <--- ALTER THE VALUE OF FLAG USING SOME MULTITHREADING SECURE FUNCTION, I.E USE A CRITICAL SECTION
  21. }
  22.  
Oct 29 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.