如何在C#中避免while循环

我试图逃避一段时间循环。 基本上,如果“如果”条件满足,我想能够退出这个循环:

private void CheckLog() { while (true) { Thread.Sleep(5000); if (!System.IO.File.Exists("Command.bat")) continue; using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat")) { string s = ""; while ((s = sr.ReadLine()) != null) { if (s.Contains("mp4:production/CATCHUP/")) { RemoveEXELog(); Process p = new Process(); p.StartInfo.WorkingDirectory = "dump"; p.StartInfo.FileName = "test.exe"; p.StartInfo.Arguments = s; p.Start(); << Escape here - if the "if" condition is met, escape the loop here >> } } } } } 

使用break; 逃避第一个循环:

 if (s.Contains("mp4:production/CATCHUP/")) { RemoveEXELog(); Process p = new Process(); p.StartInfo.WorkingDirectory = "dump"; p.StartInfo.FileName = "test.exe"; p.StartInfo.Arguments = s; p.Start(); break; } 

如果你也想逃离第二个循环,你可能需要使用一个标志并检查out循环的后卫:

  boolean breakFlag = false; while (!breakFlag) { Thread.Sleep(5000); if (!System.IO.File.Exists("Command.bat")) continue; using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat")) { string s = ""; while ((s = sr.ReadLine()) != null) { if (s.Contains("mp4:production/CATCHUP/")) { RemoveEXELog(); Process p = new Process(); p.StartInfo.WorkingDirectory = "dump"; p.StartInfo.FileName = "test.exe"; p.StartInfo.Arguments = s; p.Start(); breakFlag = true; break; } } } 

或者,如果你想从嵌套循环中完全退出函数,则return; 而不是break;

但是这些并不是最佳实践。 你应该find一些方法将必要的布尔逻辑添加到你的警卫中。

但是,您也可能想要研究一种截然不同的方法,即监听文件系统事件 。

breakgoto

 while ( true ) { if ( conditional ) { break; } if ( other conditional ) { goto EndWhile; } } EndWhile: 

如果你需要继续使用额外的逻辑…

 break; 

或者如果你有一个值返回…

 return my_value_to_be_returned; 

然而,看着你的代码,我相信你会控制循环与修改后的例子下面,而不使用中断或返回…

 private void CheckLog() { bool continueLoop = true; while (continueLoop) { Thread.Sleep(5000); if (!System.IO.File.Exists("Command.bat")) continue; using (System.IO.StreamReader sr = System.IO.File.OpenText("Command.bat")) { string s = ""; while (continueLoop && (s = sr.ReadLine()) != null) { if (s.Contains("mp4:production/CATCHUP/")) { RemoveEXELog(); Process p = new Process(); p.StartInfo.WorkingDirectory = "dump"; p.StartInfo.FileName = "test.exe"; p.StartInfo.Arguments = s; p.Start(); continueLoop = false; } } } } } 

你想退出哪个循环? 简单的break; 将退出内部循环。 对于外层循环,你可以使用一个外层循环范围的variables(例如boolean exit = false;),在你打破内层循环之前将其设置为true。 在内循环块检查退出的值,如果真正使用break; 再次。

“break”是一个突破“最接近”循环的命令。

虽然有很多好的破解方法,但是如果你不需要的话,你不应该使用它 – 它可以被看作是使用goto的另一种方式,这被认为是不好的。

例如,为什么不:

 while (!(the condition you're using to break)) { //Your code here. } 

如果您使用“break”的原因是因为您不想继续执行该循环的迭代,则可能需要使用“continue”关键字,该关键字立即跳转到循环的下一次迭代,无论是它是为了或为了。

 while (!condition) { //Some code if (condition) continue; //More code that will be skipped over if the condition was true } 

对不起necro-add,但是我真的想在现有的答案中插入缺less的东西(对于像我这样的人通过谷歌绊倒这个问题):重构你的代码。 它不仅会使读取/维护更容易,而且通常会完全去除这些types的控制路由问题。

如果我必须对上面的函数进行编程,这就是我所倾向的:

 private const string CatchupLineToIndicateLogDump = "mp4:production/CATCHUP/"; private const string BatchFileLocation = "Command.bat"; private void CheckLog() { while (true) { Thread.Sleep(5000); if (System.IO.File.Exists(BatchFileLocation)) { if (doesFileContainStr(BatchFileLocation, CatchupLineToIndicateLogDump)) { RemoveLogAndDump(); return; } } } } private bool doesFileContainStr(string FileLoc, string StrToCheckFor) { // ... code for checking the existing of a string within a file // (and returning back whether the string was found.) } private void RemoveLogAndDump() { // ... your code to call RemoveEXELog and kick off test.exe }