By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

FileSystemWatcher Created() delay

P: n/a
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works hoever
the problem I'm having is that the file does not seem to be available when I
receive this event. If I include a delay of a few seconds using Sleep(3000)
for example in the event method then the file does become available. It is
as if the application that created the file has not finished with the file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.
Jan 20 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
> Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed
Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a stream
to the disk for writing. You have written nothing to the disk. While you are
writing to the disk, the file is *being* created. When you close the handle,
the file *has been* created. Completed. Finished. Remember that "created" is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl... Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It is
as if the application that created the file has not finished with the file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.

Jan 20 '06 #2

P: n/a
Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed
Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a stream
to the disk for writing. You have written nothing to the disk. While you are
writing to the disk, the file is *being* created. When you close the handle,
the file *has been* created. Completed. Finished. Remember that "created" is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl... Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It is
as if the application that created the file has not finished with the file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.


Jan 20 '06 #3

P: n/a
Hi Tushar,

My apologies. After thinking about it, I realized that an event *may* be
raised when the file handle is created. I have scoured the .Net
documentation, and Google to find an answer, but there is no indication in
the .Net docs as to what events exactly the FileSystemWatcher is subscribing
to.

Therefore, I may be in error with my first statement.

So, let me back up a bit and see if I can be more helpful. Since it is not
documented anywhere exactly what event the FileSystemWatcher is monitoring
when a file is created, and we cannot know, perhaps we can look at your
requirements and figure out a workaround.

Your original message read:
the problem I'm having is that the file does not seem to be available
when
I
receive this event.

So, my first question is, what do you need to do with the file? It certainly
does seem that one of your requirements is that you do something with the
file. Assuming that you do need to access the file, and you state that it
"does not seem to be available," how do you know that it is not available?
That is, are you getting some kind of error? For example, if you are trying
to open the file while it is in use by another process, an exception will be
raised.

If so, I do know (this time!) that there is no way to easily tell if a file
is in use by another process. It is actually less expensive to use a
try/catch to figure this out (it could be done with unmanage API calls, but
again, that's a lot of trouble). So, one solution would be to create a loop
that has a try/catch in it. The try clause would end by exiting the loop.
The catch would put the thread to sleep for a few cycles, and continue the
loop. Something like:

int timeout = 30; // seconds to wait before giving up
while (true)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read))
{
// do something here
}
break;
}
catch
{
if (--i == 0)
throw new Exception("Unable to open file '" + filePath + "'");
Thread.Sleep(1000);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed


Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a stream
to the disk for writing. You have written nothing to the disk. While you
are
writing to the disk, the file is *being* created. When you close the
handle,
the file *has been* created. Completed. Finished. Remember that "created"
is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available
when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It
is
as if the application that created the file has not finished with the
file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore
I
could not test. If it is not the AV then I see no point if having this
API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.


Jan 20 '06 #4

P: n/a
I get the following...

The process cannot access the file "C:\...\log-Wed.txt" because it is being
used by another process IOException.

Behind the scenes .Net must be using

HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter // filter conditions to watch for
);

or

BOOL ReadDirectoryChangesW(
HANDLE hDirectory, // handle to the directory to be watched
LPVOID lpBuffer, // pointer to the buffer to receive the read results
DWORD nBufferLength, // length of lpBuffer
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter, // filter conditions to watch for
LPDWORD lpBytesReturned, // number of bytes returned
LPOVERLAPPED lpOverlapped, // pointer to structure needed for overlapped
I/O
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // pointer to
completion routine
);

Please help

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:us**************@TK2MSFTNGP15.phx.gbl...
Hi Tushar,

My apologies. After thinking about it, I realized that an event *may* be
raised when the file handle is created. I have scoured the .Net
documentation, and Google to find an answer, but there is no indication in
the .Net docs as to what events exactly the FileSystemWatcher is subscribing
to.

Therefore, I may be in error with my first statement.

So, let me back up a bit and see if I can be more helpful. Since it is not
documented anywhere exactly what event the FileSystemWatcher is monitoring
when a file is created, and we cannot know, perhaps we can look at your
requirements and figure out a workaround.

Your original message read:
the problem I'm having is that the file does not seem to be available
when
I
receive this event.

So, my first question is, what do you need to do with the file? It certainly
does seem that one of your requirements is that you do something with the
file. Assuming that you do need to access the file, and you state that it
"does not seem to be available," how do you know that it is not available?
That is, are you getting some kind of error? For example, if you are trying
to open the file while it is in use by another process, an exception will be
raised.

If so, I do know (this time!) that there is no way to easily tell if a file
is in use by another process. It is actually less expensive to use a
try/catch to figure this out (it could be done with unmanage API calls, but
again, that's a lot of trouble). So, one solution would be to create a loop
that has a try/catch in it. The try clause would end by exiting the loop.
The catch would put the thread to sleep for a few cycles, and continue the
loop. Something like:

int timeout = 30; // seconds to wait before giving up
while (true)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read))
{
// do something here
}
break;
}
catch
{
if (--i == 0)
throw new Exception("Unable to open file '" + filePath + "'");
Thread.Sleep(1000);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl... Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed


Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a stream
to the disk for writing. You have written nothing to the disk. While you
are
writing to the disk, the file is *being* created. When you close the
handle,
the file *has been* created. Completed. Finished. Remember that "created"
is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available
when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It
is
as if the application that created the file has not finished with the
file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore
I
could not test. If it is not the AV then I see no point if having this
API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.



Jan 20 '06 #5

P: n/a
Hi !

After playing a long time with the filesystemwatcher -->
A file is only perfect for processing if you can rename it.
So i store the filename in an arraylist and in a timer loop
i check
1. if the file is existing
2. if i can rename the file

if not 1) delete it from the arraylist
if not 2) try later

wolfgang

"Tushar" <xx*************@CODA.com> schrieb im Newsbeitrag
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It is
as if the application that created the file has not finished with the file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.

Jan 20 '06 #6

P: n/a
Hi Tushar,

Did you see the sameple code I posted in my last message? I suspected that
might be the case, and gave you an alternative.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:Om**************@TK2MSFTNGP14.phx.gbl...
I get the following...

The process cannot access the file "C:\...\log-Wed.txt" because it is
being
used by another process IOException.

Behind the scenes .Net must be using

HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter // filter conditions to watch for
);

or

BOOL ReadDirectoryChangesW(
HANDLE hDirectory, // handle to the directory to be watched
LPVOID lpBuffer, // pointer to the buffer to receive the read
results
DWORD nBufferLength, // length of lpBuffer
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter, // filter conditions to watch for
LPDWORD lpBytesReturned, // number of bytes returned
LPOVERLAPPED lpOverlapped, // pointer to structure needed for overlapped
I/O
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // pointer to
completion routine
);

Please help

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:us**************@TK2MSFTNGP15.phx.gbl...
Hi Tushar,

My apologies. After thinking about it, I realized that an event *may* be
raised when the file handle is created. I have scoured the .Net
documentation, and Google to find an answer, but there is no indication in
the .Net docs as to what events exactly the FileSystemWatcher is
subscribing
to.

Therefore, I may be in error with my first statement.

So, let me back up a bit and see if I can be more helpful. Since it is not
documented anywhere exactly what event the FileSystemWatcher is monitoring
when a file is created, and we cannot know, perhaps we can look at your
requirements and figure out a workaround.

Your original message read:
the problem I'm having is that the file does not seem to be available
when
I
receive this event.


So, my first question is, what do you need to do with the file? It
certainly
does seem that one of your requirements is that you do something with the
file. Assuming that you do need to access the file, and you state that it
"does not seem to be available," how do you know that it is not available?
That is, are you getting some kind of error? For example, if you are
trying
to open the file while it is in use by another process, an exception will
be
raised.

If so, I do know (this time!) that there is no way to easily tell if a
file
is in use by another process. It is actually less expensive to use a
try/catch to figure this out (it could be done with unmanage API calls,
but
again, that's a lot of trouble). So, one solution would be to create a
loop
that has a try/catch in it. The try clause would end by exiting the loop.
The catch would put the thread to sleep for a few cycles, and continue the
loop. Something like:

int timeout = 30; // seconds to wait before giving up
while (true)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read))
{
// do something here
}
break;
}
catch
{
if (--i == 0)
throw new Exception("Unable to open file '" + filePath + "'");
Thread.Sleep(1000);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed


Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a
stream
to the disk for writing. You have written nothing to the disk. While you
are
writing to the disk, the file is *being* created. When you close the
handle,
the file *has been* created. Completed. Finished. Remember that "created"
is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available
when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It
is
as if the application that created the file has not finished with the
file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore
I
could not test. If it is not the AV then I see no point if having this
API
if one is having to poll for the file to become availabe (to be closed
by
the creating application) for it to be processed by the event receiving
application.

Please help.



Jan 20 '06 #7

P: n/a

It's using FindFirstChangeNotification. The flag options correspond
one-to-one with this API. However, FSW does some additional work to ensure
the file that triggered the event matches you file template. In the API,
any file will end the wait state and return.

Mike Ober.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:Om**************@TK2MSFTNGP14.phx.gbl...
I get the following...

The process cannot access the file "C:\...\log-Wed.txt" because it is being used by another process IOException.

Behind the scenes .Net must be using

HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree DWORD dwNotifyFilter // filter conditions to watch for
);

or

BOOL ReadDirectoryChangesW(
HANDLE hDirectory, // handle to the directory to be watched
LPVOID lpBuffer, // pointer to the buffer to receive the read results DWORD nBufferLength, // length of lpBuffer
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter, // filter conditions to watch for
LPDWORD lpBytesReturned, // number of bytes returned
LPOVERLAPPED lpOverlapped, // pointer to structure needed for overlapped
I/O
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // pointer to
completion routine
);

Please help

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:us**************@TK2MSFTNGP15.phx.gbl...
Hi Tushar,

My apologies. After thinking about it, I realized that an event *may* be
raised when the file handle is created. I have scoured the .Net
documentation, and Google to find an answer, but there is no indication in
the .Net docs as to what events exactly the FileSystemWatcher is subscribing to.

Therefore, I may be in error with my first statement.

So, let me back up a bit and see if I can be more helpful. Since it is not
documented anywhere exactly what event the FileSystemWatcher is monitoring
when a file is created, and we cannot know, perhaps we can look at your
requirements and figure out a workaround.

Your original message read:
the problem I'm having is that the file does not seem to be available
when
I
receive this event.
So, my first question is, what do you need to do with the file? It certainly does seem that one of your requirements is that you do something with the
file. Assuming that you do need to access the file, and you state that it
"does not seem to be available," how do you know that it is not available?
That is, are you getting some kind of error? For example, if you are trying to open the file while it is in use by another process, an exception will be raised.

If so, I do know (this time!) that there is no way to easily tell if a file is in use by another process. It is actually less expensive to use a
try/catch to figure this out (it could be done with unmanage API calls, but again, that's a lot of trouble). So, one solution would be to create a loop that has a try/catch in it. The try clause would end by exiting the loop.
The catch would put the thread to sleep for a few cycles, and continue the
loop. Something like:

int timeout = 30; // seconds to wait before giving up
while (true)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read))
{
// do something here
}
break;
}
catch
{
if (--i == 0)
throw new Exception("Unable to open file '" + filePath + "'");
Thread.Sleep(1000);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed


Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a

stream to the disk for writing. You have written nothing to the disk. While you
are
writing to the disk, the file is *being* created. When you close the
handle,
the file *has been* created. Completed. Finished. Remember that "created" is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available
when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It
is
as if the application that created the file has not finished with the
file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this
API
if one is having to poll for the file to become availabe (to be closed by the creating application) for it to be processed by the event receiving
application.

Please help.




Jan 21 '06 #8

P: n/a
Yes I agree, the API only tells us that the directory has changed and then
we have to rely on timers etc. (polling) to process the file. An alternative
would be to read a directory on a timer as well and therefore this API does
not seem to be very useful. If one is having to rely on a timer anywhere
this API would only tell the timer code that something has changed which the
timer would have been able to ascertain anywhere using other API calls.

Thank you everyone for your responses.
"Hauer Wolfgang" <ha***@DELETETHATsysdat.at> wrote in message
news:uv**************@TK2MSFTNGP09.phx.gbl...
Hi !

After playing a long time with the filesystemwatcher -->
A file is only perfect for processing if you can rename it.
So i store the filename in an arraylist and in a timer loop
i check
1. if the file is existing
2. if i can rename the file

if not 1) delete it from the arraylist
if not 2) try later

wolfgang

"Tushar" <xx*************@CODA.com> schrieb im Newsbeitrag
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It is
as if the application that created the file has not finished with the file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore I
could not test. If it is not the AV then I see no point if having this API
if one is having to poll for the file to become availabe (to be closed by
the creating application) for it to be processed by the event receiving
application.

Please help.


Jan 23 '06 #9

P: n/a
Yes that is how I did it in the end: Stroing filenames on FileSystemWatcher
event and then trying to process the file on timer events.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
Hi Tushar,

Did you see the sameple code I posted in my last message? I suspected that
might be the case, and gave you an alternative.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:Om**************@TK2MSFTNGP14.phx.gbl...
I get the following...

The process cannot access the file "C:\...\log-Wed.txt" because it is
being
used by another process IOException.

Behind the scenes .Net must be using

HANDLE FindFirstChangeNotification(
LPCTSTR lpPathName, // pointer to name of directory to watch
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter // filter conditions to watch for
);

or

BOOL ReadDirectoryChangesW(
HANDLE hDirectory, // handle to the directory to be watched
LPVOID lpBuffer, // pointer to the buffer to receive the read
results
DWORD nBufferLength, // length of lpBuffer
BOOL bWatchSubtree, // flag for monitoring directory or directory tree
DWORD dwNotifyFilter, // filter conditions to watch for
LPDWORD lpBytesReturned, // number of bytes returned
LPOVERLAPPED lpOverlapped, // pointer to structure needed for overlapped
I/O
LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // pointer to
completion routine
);

Please help

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:us**************@TK2MSFTNGP15.phx.gbl...
Hi Tushar,

My apologies. After thinking about it, I realized that an event *may* be
raised when the file handle is created. I have scoured the .Net
documentation, and Google to find an answer, but there is no indication in
the .Net docs as to what events exactly the FileSystemWatcher is
subscribing
to.

Therefore, I may be in error with my first statement.

So, let me back up a bit and see if I can be more helpful. Since it is not
documented anywhere exactly what event the FileSystemWatcher is monitoring
when a file is created, and we cannot know, perhaps we can look at your
requirements and figure out a workaround.

Your original message read:
the problem I'm having is that the file does not seem to be available
when
I
receive this event.


So, my first question is, what do you need to do with the file? It
certainly
does seem that one of your requirements is that you do something with the
file. Assuming that you do need to access the file, and you state that it
"does not seem to be available," how do you know that it is not available?
That is, are you getting some kind of error? For example, if you are
trying
to open the file while it is in use by another process, an exception will
be
raised.

If so, I do know (this time!) that there is no way to easily tell if a
file
is in use by another process. It is actually less expensive to use a
try/catch to figure this out (it could be done with unmanage API calls,
but
again, that's a lot of trouble). So, one solution would be to create a
loop
that has a try/catch in it. The try clause would end by exiting the loop.
The catch would put the thread to sleep for a few cycles, and continue the
loop. Something like:

int timeout = 30; // seconds to wait before giving up
while (true)
{
try
{
using (FileStream fs = new FileStream(filePath, FileMode.Open,
FileAccess.Read))
{
// do something here
}
break;
}
catch
{
if (--i == 0)
throw new Exception("Unable to open file '" + filePath + "'");
Thread.Sleep(1000);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@xxCODA.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Very cleaver. I suppose my question is whether 'created' means:

1) Created directory entry (but file has no data whatsoever)
2) Completed creating the file (which is written to and closed)

I think you are suggesting it is 2) but then why is it not avilable for
reading at that stage on my PC? Why is it available after 3 seconds?
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:Os*************@TK2MSFTNGP12.phx.gbl...
Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed


Is a house created when you start digging the foundation?

When you "create" a file in software, all you have done is opened a
stream
to the disk for writing. You have written nothing to the disk. While you
are
writing to the disk, the file is *being* created. When you close the
handle,
the file *has been* created. Completed. Finished. Remember that "created"
is
the *past* tense of the verb "create." I will create. I do create. I am
creating. I created.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

"Tushar" <xx*************@CODA.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
Followup-To: microsoft.public.dotnet.general

Does anyone know when is this event raised, is it:
1) When the file is created but may not have been closed
2) When the file is created AND it has been closed

I am using this control in a Windows-Service I've developed. It works
hoever
the problem I'm having is that the file does not seem to be available
when
I
receive this event. If I include a delay of a few seconds using
Sleep(3000)
for example in the event method then the file does become available. It
is
as if the application that created the file has not finished with the
file
when the event is raised.

Could it be that the Anti-Virus gets hold of it also and checking when I
receive the event. I do not have the rights to stop the AV and therefore
I
could not test. If it is not the AV then I see no point if having this
API
if one is having to poll for the file to become availabe (to be closed
by
the creating application) for it to be processed by the event receiving
application.

Please help.




Jan 23 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.