打破无限循环所需的帮助 [英] Help needed in breaking infinite loop

查看:79
本文介绍了打破无限循环所需的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
在我的代码中,我正在使用一个无限循环while(1)
{
xxx;

}
但是现在当我按下一个按钮时,我需要打破这个循环吗?怎么做?请帮助我...谢谢您的宝贵意见.

Hi guys
in my code i am using one infinite loop while(1)
{
xxx;

}
But now i need to break this loop when i press one button ? how to do that? please help me in that ....Thanks for ur valuable comments

推荐答案

break语句使循环的执行立即结束:
The break statement causes execution of the loop to end immediately:
while(1==1)
   {
   xxx();
   if (condition)
      {
      break;
      }
   }



错别字:"1 = 1",而不是"1 == 1".感谢johannesnestler! -OriginalGriff [/edit]



[edit]Typo: "1=1" instead of "1==1". Thanks johannesnestler! - OriginalGriff[/edit]


,如果您需要压缩代码:)

if you neeed compact code :)

while(!condition)
{
   xxx();
}



在按钮的处理程序中设置condition = true



Set condition=true in button''s handler


//但是现在我需要在按下一个按钮时中断此循环

至少有两个变体:):
-循环和按键监视程序处于不同的线程中(使中断条件发生)
-循环也处理其(主)线程的消息(处理产生中断条件)
// But now i need to break this loop when i press one button

There are at least two variants :) :
- the loop and the key-watcher are in different threads (wather makes the break condition)
- the loop does process messages for its (main) thread as well (processing makes the break condition)


这篇关于打破无限循环所需的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆