473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

FileSystemWatcher and file locking?

All:

I have a .NET 1.1 C#-based WinForms application that, as part of its
normal operation, creates and updates an XML-based log file. I expose a
method from a class to retrieve the text of the file via a
StringReader, which then puts the text into a DataView that is bound to
a DataGrid. This all works terrifically.

In testing the program, I discovered certain situations in which
activities were written to the log file at unexpected times and were
thus not updated to the UI. Rather than try to determine all the points
at which a change to the log file might occur that I might have missed,
I opted to spin off a thread to monitor the log file, with the intent
of refreshing the DataView upon each change notification.

I used a FileSystemWatcher to notify the thread of change events on the
file, and therein starts my problem. When I start the application, and
perform an action that gets logged, the notification occurs, but when I
go to fetch the updated text from the file I get an "IOException - File
is in use by another process."

Now perhaps my understanding of FileSystemWatcher semantics is naive or
incorrect, but the behavior almost makes me believe the FSW is actually
holding a *lock* on the file, which to me would be counterintuitive for
a "watcher" process. Up to the point of the failure, the application is
working - event occurs, log is written, notification fires, and the
call is made to retrieve the updated log text - and that's what
crashes.

If my understanding of how FSW works is wrong, or if this is such a
naive problem that I should have found another answer by more diligent
searching, my apologies in advance. (The problems I did find that
seemed similar actually departed in certain important ways) Any
thoughts or suggestions to resolve the problem would be appreciated.

-intrepid

Aug 2 '06 #1
4 4678
My guess is that you are not opening the file as shared in when you open the
file for read/write. This would cause the file to be locked.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<in*********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
All:

I have a .NET 1.1 C#-based WinForms application that, as part of its
normal operation, creates and updates an XML-based log file. I expose a
method from a class to retrieve the text of the file via a
StringReader, which then puts the text into a DataView that is bound to
a DataGrid. This all works terrifically.

In testing the program, I discovered certain situations in which
activities were written to the log file at unexpected times and were
thus not updated to the UI. Rather than try to determine all the points
at which a change to the log file might occur that I might have missed,
I opted to spin off a thread to monitor the log file, with the intent
of refreshing the DataView upon each change notification.

I used a FileSystemWatcher to notify the thread of change events on the
file, and therein starts my problem. When I start the application, and
perform an action that gets logged, the notification occurs, but when I
go to fetch the updated text from the file I get an "IOException - File
is in use by another process."

Now perhaps my understanding of FileSystemWatcher semantics is naive or
incorrect, but the behavior almost makes me believe the FSW is actually
holding a *lock* on the file, which to me would be counterintuitive for
a "watcher" process. Up to the point of the failure, the application is
working - event occurs, log is written, notification fires, and the
call is made to retrieve the updated log text - and that's what
crashes.

If my understanding of how FSW works is wrong, or if this is such a
naive problem that I should have found another answer by more diligent
searching, my apologies in advance. (The problems I did find that
seemed similar actually departed in certain important ways) Any
thoughts or suggestions to resolve the problem would be appreciated.

-intrepid

Aug 2 '06 #2
Greg:

Thank you so much for taking the time to help.

In this particular case, I am not expressly opening the document as a
file - I'm opening it using the Load method of an XmlDocument instance.
When a transaction is logged, the XML document is opened, a transaction
added, then the document is written via the XmlDocument Save method
(back to the same filename).

Are the semantics of the Load method such that it does not open the
file shared? Or would it be better for me to expressly open the file
conventionally (shared), read it into a string, then use the string as
the parameter to the LoadXML method? This would allow me to ensure the
file is opened shared per your speculation.

Thanks again for your input.

-David

Greg Young wrote:
My guess is that you are not opening the file as shared in when you open the
file for read/write. This would cause the file to be locked.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<in*********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
All:

I have a .NET 1.1 C#-based WinForms application that, as part of its
normal operation, creates and updates an XML-based log file. I expose a
method from a class to retrieve the text of the file via a
StringReader, which then puts the text into a DataView that is bound to
a DataGrid. This all works terrifically.

In testing the program, I discovered certain situations in which
activities were written to the log file at unexpected times and were
thus not updated to the UI. Rather than try to determine all the points
at which a change to the log file might occur that I might have missed,
I opted to spin off a thread to monitor the log file, with the intent
of refreshing the DataView upon each change notification.

I used a FileSystemWatcher to notify the thread of change events on the
file, and therein starts my problem. When I start the application, and
perform an action that gets logged, the notification occurs, but when I
go to fetch the updated text from the file I get an "IOException - File
is in use by another process."

Now perhaps my understanding of FileSystemWatcher semantics is naive or
incorrect, but the behavior almost makes me believe the FSW is actually
holding a *lock* on the file, which to me would be counterintuitive for
a "watcher" process. Up to the point of the failure, the application is
working - event occurs, log is written, notification fires, and the
call is made to retrieve the updated log text - and that's what
crashes.

If my understanding of how FSW works is wrong, or if this is such a
naive problem that I should have found another answer by more diligent
searching, my apologies in advance. (The problems I did find that
seemed similar actually departed in certain important ways) Any
thoughts or suggestions to resolve the problem would be appreciated.

-intrepid
Aug 3 '06 #3
There is an overload that takes a stream
http://msdn2.microsoft.com/en-us/library/e48zttz7.aspx I would recommend
using this overload.

Cheers,

Greg

<in*********@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Greg:

Thank you so much for taking the time to help.

In this particular case, I am not expressly opening the document as a
file - I'm opening it using the Load method of an XmlDocument instance.
When a transaction is logged, the XML document is opened, a transaction
added, then the document is written via the XmlDocument Save method
(back to the same filename).

Are the semantics of the Load method such that it does not open the
file shared? Or would it be better for me to expressly open the file
conventionally (shared), read it into a string, then use the string as
the parameter to the LoadXML method? This would allow me to ensure the
file is opened shared per your speculation.

Thanks again for your input.

-David

Greg Young wrote:
>My guess is that you are not opening the file as shared in when you open
the
file for read/write. This would cause the file to be locked.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<in*********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegr oups.com...
All:

I have a .NET 1.1 C#-based WinForms application that, as part of its
normal operation, creates and updates an XML-based log file. I expose a
method from a class to retrieve the text of the file via a
StringReader, which then puts the text into a DataView that is bound to
a DataGrid. This all works terrifically.

In testing the program, I discovered certain situations in which
activities were written to the log file at unexpected times and were
thus not updated to the UI. Rather than try to determine all the points
at which a change to the log file might occur that I might have missed,
I opted to spin off a thread to monitor the log file, with the intent
of refreshing the DataView upon each change notification.

I used a FileSystemWatcher to notify the thread of change events on the
file, and therein starts my problem. When I start the application, and
perform an action that gets logged, the notification occurs, but when I
go to fetch the updated text from the file I get an "IOException - File
is in use by another process."

Now perhaps my understanding of FileSystemWatcher semantics is naive or
incorrect, but the behavior almost makes me believe the FSW is actually
holding a *lock* on the file, which to me would be counterintuitive for
a "watcher" process. Up to the point of the failure, the application is
working - event occurs, log is written, notification fires, and the
call is made to retrieve the updated log text - and that's what
crashes.

If my understanding of how FSW works is wrong, or if this is such a
naive problem that I should have found another answer by more diligent
searching, my apologies in advance. (The problems I did find that
seemed similar actually departed in certain important ways) Any
thoughts or suggestions to resolve the problem would be appreciated.

-intrepid

Aug 3 '06 #4
Greg:

This looks like an excellent potential solution for this problem.

Thank you again for your assistance.

-David

Greg Young wrote:
There is an overload that takes a stream
http://msdn2.microsoft.com/en-us/library/e48zttz7.aspx I would recommend
using this overload.

Cheers,

Greg

<in*********@hotmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Greg:

Thank you so much for taking the time to help.

In this particular case, I am not expressly opening the document as a
file - I'm opening it using the Load method of an XmlDocument instance.
When a transaction is logged, the XML document is opened, a transaction
added, then the document is written via the XmlDocument Save method
(back to the same filename).

Are the semantics of the Load method such that it does not open the
file shared? Or would it be better for me to expressly open the file
conventionally (shared), read it into a string, then use the string as
the parameter to the LoadXML method? This would allow me to ensure the
file is opened shared per your speculation.

Thanks again for your input.

-David

Greg Young wrote:
My guess is that you are not opening the file as shared in when you open
the
file for read/write. This would cause the file to be locked.

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<in*********@hotmail.comwrote in message
news:11*********************@b28g2000cwb.googlegro ups.com...
All:

I have a .NET 1.1 C#-based WinForms application that, as part of its
normal operation, creates and updates an XML-based log file. I expose a
method from a class to retrieve the text of the file via a
StringReader, which then puts the text into a DataView that is bound to
a DataGrid. This all works terrifically.

In testing the program, I discovered certain situations in which
activities were written to the log file at unexpected times and were
thus not updated to the UI. Rather than try to determine all the points
at which a change to the log file might occur that I might have missed,
I opted to spin off a thread to monitor the log file, with the intent
of refreshing the DataView upon each change notification.

I used a FileSystemWatcher to notify the thread of change events on the
file, and therein starts my problem. When I start the application, and
perform an action that gets logged, the notification occurs, but when I
go to fetch the updated text from the file I get an "IOException - File
is in use by another process."

Now perhaps my understanding of FileSystemWatcher semantics is naive or
incorrect, but the behavior almost makes me believe the FSW is actually
holding a *lock* on the file, which to me would be counterintuitive for
a "watcher" process. Up to the point of the failure, the application is
working - event occurs, log is written, notification fires, and the
call is made to retrieve the updated log text - and that's what
crashes.

If my understanding of how FSW works is wrong, or if this is such a
naive problem that I should have found another answer by more diligent
searching, my apologies in advance. (The problems I did find that
seemed similar actually departed in certain important ways) Any
thoughts or suggestions to resolve the problem would be appreciated.

-intrepid
Aug 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
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...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
1
by: Olaf Rabbachin | last post by:
Hi folks, I have an application with a separate thread that copies files between directories. In the GUI-thread, I have a FileSystemWatcher running that monitors the target-directory the second...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
5
by: Goran Djuranovic | last post by:
Hi all, I have a file system watcher service that works fine on a local hard drive, but will not work across the network. I tried both: mapping the drive and "\\..." path both no luck. I don't...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
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...

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.