c# - 如何跳出循环

标签 c# .net loops

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

相关文章:

c++ - 通过循环 C++ 打印报价单

c# - 标记接口(interface) : make them inter-castable

c# - 记录具有保存敏感数据(待散列)属性的对象

c# - 在服务器应用程序中将中间证书与 SslStream 和 X509Certificate2 一起使用

python - 使用 for 循环迭代并引用 lst[i] 时出现 TypeError/IndexError

javascript - 循环和数组问题

c# - 预定义单词集中包含的单词的正则表达式

c# - 使用 Entity Framework 获取数据长度(以验证 WCF 服务上的数据长度)

c# - VS 强制更新到 2.1.4 后,.net core-mvc 应用程序将不会以 2.1.3 为目标

.Net 安装程序 - .exe 和 .msi 之间的差异