6

I would like to read a file after I have been notified that a certain amount of data has been written to it via another thread.

My intial attempt was to create a Reactive Subject in my Writer class that calls OnNext after the Write to the BinaryWriter it is composed with. This BinaryWriter uses a FileStream.
This does not seem to work though. I am assuming I'm not guaranteed that the Write has been flushed.

I'd rather not manually call flush. Is there an existing way to do this?

Fredrick
  • 1,210
  • 2
  • 15
  • 24
  • 4
    Congratulations on asking the question number 5,000,000! – Sergey Kalinichenko May 09 '13 at 15:49
  • 1
    Does [File System Watcher](http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx) help? – Elias May 11 '13 at 06:15
  • Isn't there any prize for who asks the question numbered 5,000,000 or next 6,000,000 or ...? I think s/he should be worth receiving 100 points of reputation. – King King May 11 '13 at 06:31
  • This can be duplicated answer according http://stackoverflow.com/questions/1409492/read-from-a-growing-file-in-c – Shahar G. May 11 '13 at 12:22
  • I don't believe FileSystemWatcher will give me the event granularity I'm looking for. I don't think there is simple way to know when data written via FileStream will be available on disk. Currently, I am just polling the disk until I can invest in a higher level solution. – Fredrick May 14 '13 at 14:01

1 Answers1

1

At a broader level, you're asking about communications between threads. There are a couple of ways to handle this depending upon your preferences and environment.

If you haven't already, look over POSIX threading here and here. Pay attention to the sections discussing conditional variables or semaphores.

The .NET framework has a System Threading library that's also worth looking over. In particular, it has both a semaphore and a monitor class. One or the other may be the solution you need in this case.

Finally, consider the InotifyPropertyChanged interface and / or events. In this context, it's an alternate route to signaling progress from the reader.

In general, I'd recommend having your writer thread throw an event changed notification or raise a semaphore after writing X bytes. From there, the reader will either catch the event or watch for the semaphore and act accordingly.

I suspect the monitor class won't be as useful for you since the locking will likely prevent simultaneous access to the file.