如何逃避循环

Ben*_*Ben 4 .net c# 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();
}
Run Code Online (Sandbox Code Playgroud)

axe*_*l_c 6

使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;
      }      
}
Run Code Online (Sandbox Code Playgroud)