Reputation: 341
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();
}
Upvotes: 4
Views: 527
Reputation: 6796
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;
}
}
Upvotes: 6
Reputation: 10381
Have ScanChanges
return a bool
if you've reached that if
statement within ScanChanges
, and then have another if
statement in the while loop that skips over those two procedures if ScanChanges
comes back true.
Upvotes: 3
Reputation: 5042
Make the return value out of ScanChanges, it may be boolean if it going to break loop return true else return false.
Then set break loop condition in main.
Upvotes: 0