2

for reference I have looked at Is there a way to check if a file is in use? and How to wait until File.Exists?

but I want to avoid using SystemWatcher as it seems kind of overdoing. My application is calling cmd prompt to create a file and as there is no ways for my application to know when it is finished I was thinking of using Sleep() as long as the file does not exist.

string filename = @"PathToFile\file.exe";
int counter = 0;
while(!File.Exists(filename))
{
    System.Threading.Thread.Sleep(1000);
    if(++counter == 60000)
    {
        Logger("Application timeout; app_boxed could not be created; try again");
        System.Environment.Exit(0);
    }
}

Somehow this code of mine does not seem to work. What could be the cause?

slavoo
  • 5,798
  • 64
  • 37
  • 39
sceiler
  • 1,145
  • 2
  • 20
  • 35
  • 12
    I guarantee `SystemWatcher` will use less resources then any method you come up with involving sleep and while loops – Icemanind Oct 09 '14 at 19:05
  • 3
    This sounds like the *exact purpose* of `FileSystemWatcher`. If the code you wrote isn't working, and `FileSystemWatcher` does work, it's not really "overdoing" things. – David Oct 09 '14 at 19:06
  • 2
    What happen? _does not seem to work_ is too vague. – Steve Oct 09 '14 at 19:06
  • So did you wait the 60000 seconds? – 500 - Internal Server Error Oct 09 '14 at 19:18
  • I am so confused too why you have a counter and you sleep every second waiting for the counter to reach 60000. You could eliminate all that just by putting `System.Threading.Thread.Sleep(60000000)`. However, what I think you wanted was `System.Threading.Thread.Sleep(60000)`. – Icemanind Oct 09 '14 at 19:24
  • FileSystemWatcher won't work on a network path, IIRC. – x0n Oct 09 '14 at 19:30
  • @x0n - FileSystemWatcher most certainly will work on a network path, using UNC. However, sometimes getting the permissions set correctly can be a hassle. – Icemanind Oct 09 '14 at 19:32
  • oh thanks for pointing out. yeah the counter should be 60 so at most wait for 1 minute and if the job isnt done terminate app. I rely on a file created with cmd and afterwards that file will be renamed and proceeded further. as the sleep doesnt work my file will be deleted and cant be proceeded further resulting in my application to terminate. – sceiler Oct 09 '14 at 21:03

1 Answers1

6

Not sure what part isn't working. Did you realize that your loop will run for 60,000 seconds (16.67 hours)? You are incrementing it once per second and waiting for it to reach 60000.

Try something like this:

const string filename = @"D:\Public\Temp\temp.txt";

// Set timeout to the time you want to quit (one minute from now)
var timeout = DateTime.Now.Add(TimeSpan.FromMinutes(1));

while (!File.Exists(filename))
{
    if (DateTime.Now > timeout)
    {
        Logger("Application timeout; app_boxed could not be created; try again");
        Environment.Exit(0);
    }

    Thread.Sleep(TimeSpan.FromSeconds(1));
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • nice I like your code more than mine. I made a mistake with the time and corrected other part of the code so it works now. – sceiler Oct 09 '14 at 21:32