Access to file

  • Thread starter Thread starter Christian Havel
  • Start date Start date
C

Christian Havel

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
 
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)
 
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.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top