我试图逃跑while循环。基本上,如果如果条件满足,我希望能够退出这个循环:
私人无效CheckLog()
{
而(真)
{
Thread.sleep代码(5000);
如果(!System.IO.File.Exists(Command.bat))
继续;
使用(就是System.IO.StreamReader SR = System.IO.File.OpenText(Command.bat))
{
字符串s =;
而((S = sr.ReadLine())!= NULL)
{
如果(s.Contains(MP4:生产/ CATCHUP /))
{
RemoveEXELog();
进程p =新的Process();
p.StartInfo.WorkingDirectory =倾销;
p.StartInfo.FileName =TEST.EXE;
p.StartInfo.Arguments =秒;
p.Start();
<<逃离这里 - 如果,如果条件满足,在这里和GT逃脱循环;>
}
}
}
}
}
解决方案
使用突破;
逃跑的第一个循环:
如果(s.Contains(MP4:生产/ CATCHUP /))
{
RemoveEXELog();
进程p =新的Process();
p.StartInfo.WorkingDirectory =倾销;
p.StartInfo.FileName =TEST.EXE;
p.StartInfo.Arguments =秒;
p.Start();
打破;
}
如果您也想逃避第二个循环,你可能需要使用一个标志,并检查出环路的后卫:
布尔breakFlag = FALSE;
而(!breakFlag)
{
Thread.sleep代码(5000);
如果继续(System.IO.File.Exists(Command.bat)!);
使用(就是System.IO.StreamReader SR = System.IO.File.OpenText(Command.bat))
{
字符串s =;
而((S = sr.ReadLine())!= NULL)
{
如果(s.Contains(MP4:生产/ CATCHUP /))
{
RemoveEXELog();
进程p =新的Process();
p.StartInfo.WorkingDirectory =倾销;
p.StartInfo.FileName =TEST.EXE;
p.StartInfo.Arguments =秒;
p.Start();
breakFlag = TRUE;
打破;
}
}
}
或者,如果你想只完全退出功能从嵌套循环,放于返回之内;
,而不是打破;
。
但这些都没有真正考虑的最佳实践。你应该找到一些方法来添加必要的布尔逻辑到你的,而
卫兵。
I am trying to escape a while loop. Basically, if the "if" condition is met, I would like to be able to exit this loop:
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 >>
}
}
}
}
}
解决方案
Use break;
to escape the first loop:
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;
}
If you want to also escape the second loop, you might need to use a flag and check in the out loop's guard:
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;
}
}
}
Or, if you want to just exit the function completely from within the nested loop, put in a return;
instead of a break;
.
But these aren't really considered best practices. You should find some way to add the necessary Boolean logic into your while
guards.