473,466 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Create 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 7086
> 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
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...
2
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...
4
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...
3
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...
3
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...
0
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....
2
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...
5
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,...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.