gpt4 book ai didi

c# - 如何跳出循环

转载 作者:太空狗 更新时间:2023-10-30 00:27:29 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com