我在 Main() 中有一个 while 循环,它通过几种方法。虽然一个名为 ScanChanges()
的方法有一个 if/else 语句,但在 if
的情况下,它必须跳转到 Thread.Sleep(10000)
(在循环结束时)。
static void Main(string[] args)
{
while (true)
{
ChangeFiles();
ScanChanges();
TrimFolder();
TrimFile();
Thread.Sleep(10000);
}
}
private static void ChangeFiles()
{
// code here
}
private static void ScanChanges()
{
}
FileInfo fi = new FileInfo("input.txt");
if (fi.Length > 0)
{
// How to Escape loop??
}
else
{
Process.Start("cmd.exe", @"/c test.exe -f input.txt > output.txt").WaitForExit();
}
最佳答案
让 ScanChanges 返回一些值,指示您是否必须跳到循环的末尾:
class Program
{
static void Main(string[] args)
{
while (true)
{
ChangeFiles();
bool changes = ScanChanges();
if (!changes)
{
TrimFolder();
TrimFile();
}
Thread.Sleep(10000);
}
}
private static void ChangeFiles()
{
// code here
}
private static bool ScanChanges()
{
FileInfo fi = new FileInfo("input.txt");
if (fi.Length > 0)
{
return true;
}
else
{
Process.Start("cmd.exe", @"/c test.exe -f input.txt > output.txt").WaitForExit();
return false;
}
}
关于c# - 如何跳出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6396888/