Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Magic FileStream Caching

Status
Not open for further replies.

Tarwn

Programmer
Mar 20, 2001
5,787
US
I have a small application that has a timer with a 10s interval. The event off this timer instantiates a local FileStream object, reads the file, then closes the stream.
The file I am reading changes every few minutes (it's basically a dump of the current song from a music player). The problem I am running into is that after about 10 minutes the FileStream starts supplying old content. I have placed break points in the file and simultaneously pulled up the current content of the file and the FileStream will be reporting content from several changes back (10-15 minutes prior).
I'm wondering if anyone has run into this issue before or if I am missing the FileStream.OpenTheDangFile_NoReally() function.

The function that opens the filestream is as follows:
Code:
 Try
            Dim fs As New IO.FileStream(txtFilePath.Text, IO.FileMode.Open, System.IO.FileAccess.Read)
            Dim sr As New IO.StreamReader(fs)
            Dim s As String = sr.ReadToEnd()
            sr.Close()
            fs.Close()

            txtData.Lines = s.Split(vbLf)
        Catch ex As Exception
            txtData.Lines = New String() {"VB.Net Exception", "Arrrgh", "Arrrgh"}
        End Try

These vasriables are local to the function and there are no other variables with the same names in the app, so I don't believe it is a scope issue.

The really nifty thing is that if I stop and Start the Timer object it will actually start picking up the real file again for a while, then it will stop and continue to read the same copy of that file no matter how much the file changes.

barcode_1.gif
 
You could try:

sr.Close()
fs.Close()
sr = Nothing
fs = Nothing


Hope this helps.

[vampire][bat]
 
Alright, I played with it more and it seems like the missing piece of my explanation above is that the file I'm reading is on a mapped drive. The same app reading a constantly changing local file works fine as far as I can tell, so it may not be a .Net issue at all. Still curious if anyone else has seen this. Neither machine has lost connection or anything (wired to same switch) but I have to believe that Windfows is trying to helpfully cache content for me or something. The only thing thats wierd is that it consistently reconnects to the file just by me restarting the timer control.

barcode_1.gif
 
perhaps it's a timer issue then. Try one of the others, I prefer the thread one. I'm pretty sure windows caches the files and I'm even more sure that you can turn it of, you just have to find the switch.

Christiaan Baes
Belgium

"My new site" - Me
 
I thought of that, which is why I added a breakpoint and started viewing the values in the watch window...the string (contents of the file) that I created in the funciton was poepulated with the same value every time (ie, song #1, song#2, #3, #3, #3, #3, #3, etc)

Ended up leaving work before I could put together a threaded Python class. I wanted to see if this was just .Net and mapped drives or strictly a mapped drives issue.

barcode_1.gif
 
I'm fairly certain the FileStream has one (don't have it in front of me). Usually Flush methods are for flushing anything you have currently written to the stream buffer. I'll take a look in a couple hours when I get to work.

barcode_1.gif
 
Nope, no Flush on the StreamReader.

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top