473,537 Members | 2,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatcher Created() delay

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
9 7102
> 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
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
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
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
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
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2387
by: Phil Galey | last post by:
I have a FileSystemWatcher that triggers when a PDF file is created. However, the creation of the PDF file is about a 7 or 8 second process ... I cannot refer to the file during that time because it does not yet exist. If I set up a loop using the Sleep API call, it loops forever and the software that creates the PDF file stops progressing...
2
3207
by: Bill | last post by:
I created a Windows Service (written in VB.NET) that uses a FileSystemWatcher to monitor a directory for file creation. When files with a certain extension are created in the directory, the file is opened and read (something is done with the contents). Then the file is moved to a backup directory. Everything works fine when the coe runs as a...
4
3709
by: Doug R | last post by:
Hello, I could use a little help from you Gurus out there. I have an aplication that watches a directory and detects when a PGP encrypted file lands in the directory and starts a process that decrypts it and parses the resulting contents. I am using the FileSystemWatcher's Created event to detect when the file arrives. The problem I have...
3
1579
by: Florida Development | last post by:
I have the need to monitor a directory for the arrival of files and then to run some processing on those new files. This is easy enough conceptually, the problem I have is that I get an event to handle when the file is first created yet is not fully written to the disk yet. Is there some simple way (that will pass a code review) that I can...
3
14229
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started a working thread for processing the file, created a new FileSystemWatcher (as he said for real time processing), and then called the join method for...
0
1079
by: Ash | last post by:
I have Server A & Server B. A process that implements FileSystemWatcher on Server B is watching for File Created events on Server A using something like \\ServerA\c$\MyFiles . Server A got reboot. How come no exceptions where thrown in the FileSystemWatcher processes running on Server B?? Did anybody notice that. Am i implementing somthing...
2
1712
by: Ripley | last post by:
I am trying to find out exactly when this even fires when a file is "created" in a directory being monitored. That is, for a large file, that takes several minutes to upload, will the created event fire as soon as the file starts loading, or only when the file has been completely uploaded. I can't find any documentation to this at MSDN. ...
5
5553
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher, monitors a folder and then returns the contents of the new/changed files. However, if the client app loses connection to the webservice without...
0
7365
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7301
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7281
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5827
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5221
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3353
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1761
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.