467,926 Members | 2,123 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

file system watcher question

i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.

Nov 16 '05 #1
  • viewed: 1828
Share:
5 Replies
What you could to do is contain the monitor with in a while loop that will
exit when processed once.

Inside this loop have a try catch statement that catches the exception that
is thrown when a locked file is accessed. At this point you could then wait
for a certain amount of time, then try to access the file again, and if this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:

int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
"ToddT" <tu********@maritzSPAM.com> wrote in message
news:s3********************************@4ax.com...
i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.

Nov 16 '05 #2
thx for the reply.

yeah, that is pretty much what i am doing. it just feels like a hack.
i wish there was a "close file" event thrown by the fsw...
On Thu, 30 Dec 2004 09:27:17 -0000, "Dan Bass" <danielbass [at]
postmaster [dot] co [dot] uk> wrote:
What you could to do is contain the monitor with in a while loop that will
exit when processed once.

Inside this loop have a try catch statement that catches the exception that
is thrown when a locked file is accessed. At this point you could then wait
for a certain amount of time, then try to access the file again, and if this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:

int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
"ToddT" <tu********@maritzSPAM.com> wrote in message
news:s3********************************@4ax.com.. .
i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.


Nov 16 '05 #3
That would be nice. I suppose it's because the purpose behind the file
system watcher is for logging files that have been moved/rename/created, and
not for analysing the file details.

"ToddT" <tu********@maritzSPAM.com> wrote in message
news:25********************************@4ax.com...
thx for the reply.

yeah, that is pretty much what i am doing. it just feels like a hack.
i wish there was a "close file" event thrown by the fsw...
On Thu, 30 Dec 2004 09:27:17 -0000, "Dan Bass" <danielbass [at]
postmaster [dot] co [dot] uk> wrote:
What you could to do is contain the monitor with in a while loop that will
exit when processed once.

Inside this loop have a try catch statement that catches the exception
that
is thrown when a locked file is accessed. At this point you could then
wait
for a certain amount of time, then try to access the file again, and if
this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:

int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
"ToddT" <tu********@maritzSPAM.com> wrote in message
news:s3********************************@4ax.com. ..
i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.

Nov 16 '05 #4
Dax
I came up with the same work around. I too feel like it's a hack. I
eventually dropped the system watcher for my own home grown version. I also
found it a little buggy on Windows Server 2000.

Dax

"ToddT" <tu********@maritzSPAM.com> wrote in message
news:25********************************@4ax.com...
thx for the reply.

yeah, that is pretty much what i am doing. it just feels like a hack.
i wish there was a "close file" event thrown by the fsw...
On Thu, 30 Dec 2004 09:27:17 -0000, "Dan Bass" <danielbass [at]
postmaster [dot] co [dot] uk> wrote:
What you could to do is contain the monitor with in a while loop that will
exit when processed once.

Inside this loop have a try catch statement that catches the exception
that
is thrown when a locked file is accessed. At this point you could then
wait
for a certain amount of time, then try to access the file again, and if
this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:

int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
"ToddT" <tu********@maritzSPAM.com> wrote in message
news:s3********************************@4ax.com. ..
i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.

Nov 16 '05 #5
Yep, I don't used the file system watcher either. I reverted to a clunky
polling system. Hey, it works. ;o)

"Dax" <ac*******@online.nospam> wrote in message
news:eg**************@TK2MSFTNGP15.phx.gbl...
I came up with the same work around. I too feel like it's a hack. I
eventually dropped the system watcher for my own home grown version. I also
found it a little buggy on Windows Server 2000.

Dax

"ToddT" <tu********@maritzSPAM.com> wrote in message
news:25********************************@4ax.com...
thx for the reply.

yeah, that is pretty much what i am doing. it just feels like a hack.
i wish there was a "close file" event thrown by the fsw...
On Thu, 30 Dec 2004 09:27:17 -0000, "Dan Bass" <danielbass [at]
postmaster [dot] co [dot] uk> wrote:
What you could to do is contain the monitor with in a while loop that
will
exit when processed once.

Inside this loop have a try catch statement that catches the exception
that
is thrown when a locked file is accessed. At this point you could then
wait
for a certain amount of time, then try to access the file again, and if
this
files, wait again etc... until you have access to the file.

so it may look something like this in the creation event handler:

int secondsToSleep = 5;
bool fileAccessed = false;

while ( !fileAccessed )
{
try
{
//
// try to read the file contents here
//

// successful read
fileAccessed = true;
}
catch ( UnauthorizedAccessException ex ) // is this the exception
thrown?
{
// could not access the file
System.Threading.Thread.Sleep ( secondsToSleep * 1000 );
}

}
"ToddT" <tu********@maritzSPAM.com> wrote in message
news:s3********************************@4ax.com ...
i've got one app that writes large files to a specific directory that
is watched by another app via an instance of the file system watcher
class. my problem is that the second app is notified when files are
created, but the first app hasn't finished writing to it - causing a
"file is already open" error when the second app trys to open the file
for processing. what i need is a "file closed" event to be thrown by
the file system watcher object. any idea how i can get around this
problem? thx.


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ron Vecchi | last post: by
7 posts views Thread by Allen Anderson | last post: by
reply views Thread by Jack David | last post: by
2 posts views Thread by Jack David | last post: by
2 posts views Thread by ToddT | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.