How to avoid a cycle

I have a while loop in Main () that goes through several methods. Although one method named ScanChanges() has an if / else statement, if it should go 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(); } 
+4
source share
4 answers

Make ScanChanges return some value indicating whether to 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; } } 
+6
source

Return the ScanChanges bool if you have reached this if in ScanChanges , and then add another if in the while loop, which skips these two procedures if ScanChanges comes back true.

+3
source

Print the return value from ScanChanges, it can be logical if it breaks the loop return true else return false.

Then set the interrupt condition to main.

0
source

Use break to exit the loop.

 if (fi.Length > 0) { break; } 
0
source

All Articles