Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a loop

Tags:

c#

.net

loops

I have a while loop in Main() which goes through several methods. Although one method named ScanChanges() has an if / else statement, in the case of if, it must jump to Thread.Sleep(10000) (at the end of the loop).

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();
}
like image 685
Ben Avatar asked Jan 20 '23 11:01

Ben


1 Answers

Make ScanChanges return some value indicating whether you must skip to the end of the loop:

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;
      }      
}
like image 119
axel_c Avatar answered Jan 29 '23 00:01

axel_c