Check if File exists

R

Ricardo Luceac

Hi all.

I'm having a problem with this, I have look if a file exists, if don't wait
till it is created and if it exists I need to open it. I do the following:


for (; ; )

{

if (fl.Exists)

{

StreamReader sr = fl.OpenText();

StreamWriter sw = new StreamWriter(FILE_NAME);

String line;

while ((line = sr.ReadLine()) != null)

{

if (!line.Contains("Pagina"))

sw.WriteLine(line);

}

sr.Close();

sw.Close();

}

}



The problem is that when it's checking if the file exists the cpu load is in
100%.

Is there a way to do that check once per second?? like an wait 1 second them
check it again??

Or any other method???



Thx..
 
N

Nicholas Paldino [.NET/C# MVP]

Ricardo,

You could just put the thread to sleep, or have a timer that fires every
minute, but this is an expensive operation.

Check out the FileSystemWatcher class. You can monitor a directory for
changes, and when there are changes, your code will be notified. It is less
intensive than looping and checking the existence of a file. Instead, you
will just be notified when there is a change, and if it is the file you are
looking for, then you can process it.

Hope this helps.
 
D

David Hernandez Diez

Hello,

The problem I see is that the loop is continuously looping without
stopping, that's why the cpu is at 100%.

Insert just before the end of the for loop:

Thread.Sleep(1000); //1000 = 1 second

With this method the thread will wait one second, before checking the
file again
 
C

Craig Lister

Is there no c# way of saying...

while fileexists=false....
{
// Do nothing...
}

?
 
J

Jon Skeet [C# MVP]

Nicholas Paldino said:
You could just put the thread to sleep, or have a timer that fires every
minute, but this is an expensive operation.

Check out the FileSystemWatcher class. You can monitor a directory for
changes, and when there are changes, your code will be notified. It is less
intensive than looping and checking the existence of a file. Instead, you
will just be notified when there is a change, and if it is the file you are
looking for, then you can process it.

In my experience, however, it's hard if not impossible to get
FileSystemWatcher to work reliably. When trying to spot a file being
renamed (and then left alone) I tended to get no event when the first
file was renamed, then two (one for each file) when a second file was
renamed. Unfortunately I never managed to reproduce the problem in a
unit test, but it cropped up often enough in reality to add an extra
"just check every so often in case" test.

I seem to remember others hearing about FSW "dropping" events too...
 
J

Jon Skeet [C# MVP]

Craig Lister said:
Is there no c# way of saying...

while fileexists=false....
{
// Do nothing...
}

Yes, there is. But how often do you check? Is your "do nothing" meant
to be a sleep, or did you really mean to go back to the test
immediately? If so, that's a tight loop which will absolutely *hammer*
the CPU until the file is created - and if whatever's creating that
file is CPU-bound, it could take a long time to arrive...
 
C

Craig Lister

Agreed. Aye, a sleep would be needed... and.. not sure how you do it in C#
yet, but in Delphi, maybe a Application.ProcessMessages; which basically
refreshes the screen for any changes...
 
J

Jon Skeet [C# MVP]

Craig said:
Agreed. Aye, a sleep would be needed...

Using Thread.Sleep. However, that brings up the question of "How
often"? Using a FileSystemWatcher gets you more immediate feedback -
but only when it works...

(While slightly more complicated, I quite like an approach of using
FileSystemWatcher and also polling once a minute. The overhead from
that polling is minimal, but we still get good response when the
FileSystemWatcher is working.)
and.. not sure how you do it in C#
yet, but in Delphi, maybe a Application.ProcessMessages; which basically
refreshes the screen for any changes...

You'd only need that if this code were running in the UI thread, which
I would hope it's not.
(The equivalent code is Application.DoEvents, which should be a real
rarity to see in C# code.)

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

The problem is that when it's checking if the file exists the cpu load is
in 100%.

Why?
File.Exist should not be that resource intensive.
Is there a way to do that check once per second?? like an wait 1 second
them check it again??

I use a mix approach, I use FileSystemWatcher to know when the file is
created, but sometimes (very often) the file is still in use when I get this
event and therefore I cannot access it.
so for this I put the thread to sleep for a while and then I try again until
I can open it or a number of intent are done, in the latter an exception is
throw.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jon Skeet said:
In my experience, however, it's hard if not impossible to get
FileSystemWatcher to work reliably.

Totally agree with you, sometimes you get more than one notification per
user single action, of course this is by design and there is nothing that
can be done to improve this :(

I seem to remember others hearing about FSW "dropping" events too...

I have never had this problem, quite the opposite , my problem is taht I
get more than one events.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Jon Skeet said:
Using Thread.Sleep. However, that brings up the question of "How
often"? Using a FileSystemWatcher gets you more immediate feedback -
but only when it works...

(While slightly more complicated, I quite like an approach of using
FileSystemWatcher and also polling once a minute. The overhead from
that polling is minimal, but we still get good response when the
FileSystemWatcher is working.)

I do use this approach too, but with one addition too, as I said before
sometimes you get duplicated events for a single user action. so I also
check a timespan when handling the same event, this assure me that I'm
handling a different event.
Of course this works only if you can assure that an action will not happen
inside the timespan.

OP: do you control the creation of the file?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top