Hi,
I have the following situation:
-> app1 writes data to a file
-> app2 (a service) is notified when the file is changed using the
FileSystemWatcher
-> app2 reads the last added data and should remove the read data from the
file
Well, I think I can get a problem, if app1 tries to write data to the file,
while app2 is removing the content from the file at the same time. How can
this problem be solved?
Christian
Ignacio Machin ( .NET/ C# MVP ) - 18 Jan 2008 16:00 GMT
Hi,
Can you use some other mechanism than a file?
Otherwise, can you create separated files each time App1 has something to
write (App2 can delete the consumed files afterwards)

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> Hi,
>
[quoted text clipped - 11 lines]
> this problem be solved?
> Christian
Olie - 18 Jan 2008 16:16 GMT
For this very reason, when you try and write to a file the operating
system will lock the file preventing other applications from using it.
I think you can use the FileSystemMonitor to tell you when this lock
is removed. Then you know when it is safe to try and write to it.
The other way to do it is to ensure that only app1 actually writes to
the file. You could do this by having another file written to by app2
telling app1 what data needs to be deleted.
Christian Havel - 18 Jan 2008 16:38 GMT
Thanks for your help.
Is the file locked if I only read from it, too?
Christian
> For this very reason, when you try and write to a file the operating
> system will lock the file preventing other applications from using it.
[quoted text clipped - 4 lines]
> the file. You could do this by having another file written to by app2
> telling app1 what data needs to be deleted.
Olie - 18 Jan 2008 16:51 GMT
The file lock allows multiple readers but only one writer.
When the file is locked for writing only that application may write or
read from the file.
When a file is locked for reading any application can read from that
file and no application can write to it.
Christian Havel - 21 Jan 2008 08:29 GMT
Thanks Olie :-)
> The file lock allows multiple readers but only one writer.
>
[quoted text clipped - 3 lines]
> When a file is locked for reading any application can read from that
> file and no application can write to it.