Problems with Stream reader. File being used by another process

brandonyoung

Member
Joined
Apr 26, 2011
Messages
12
Programming Experience
3-5
To cut a long story short I am having problems with stream reader and a text file being locked. On a button click I am trying to read from a file, then add a integer to that read value and then write the new value to the same file.

Not always but, it sometimes throws an exception. Id say every 100 times or so. It says that the file is being used by another process. Why is there inconsistency? Does a stream flush and close on command or is there a delay before it actually does?

I first tried to use .flush() instead of .Close(), and then used both. I then used System.Threading.Thread.Sleep(5000) in hope that it just wasnt having time to read from the file, close it then write to the same file. Neither of the above worked.

Iv now opened the file and written to the file using FileShare.ReadWrite. This seems to help but still throws an exception every once in a while.

What can I do and why is it not working???? Im using visual studio, could it be a bug in visual studio or even streamreader/writer?
Here is my code below




FIRST THIS FUNCTION IS CALLED


VB.NET:
 Public Function UpdateDailyNonRegTimeOUT()
DailyNonRegTimeValue = (CurrentDailyNonRegTimeValue + DailyNonRegTimeValue)
 
Dim haveExclusiveAccess As Boolean = False
While haveExclusiveAccess = False
 
Try
Dim sFileName As String = "C:/School/Data/Children/" + ChildsNameOUT + "/Daily Non Registered Time/" + CStr(ThisYear) + "/" + CStr(ThisMonth) + "/" + CStr(TodaysDate) + ".txt"
 
Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
'Create the stream writer
 
Dim myWriter As New System.IO.StreamWriter(myFileStream)
'Write in what is in the text box
 
myWriter.WriteLine(CStr(DailyNonRegTimeValue))
 
'Flush before we close
myWriter.Flush()
 
'Close everything
myWriter.Close()
 
myFileStream.Close()
haveExclusiveAccess = True
 
Catch ex As Exception
MsgBox("1212 " + ex.ToString)
haveExclusiveAccess = False
 
End Try
 
End While
 
Return 1
End function


THEN THIS FUNCTION IS CALLED


VB.NET:
Public Function UpdateDailyNonRegTimeOUT()
 
Dim haveExclusiveAccess As Boolean = False
While haveExclusiveAccess = False
 
Try
Dim sFileName As String = "C:/School/Data/Children/" + ChildsNameOUT + "/Daily Non Registered Time/" + CStr(ThisYear) + "/" + CStr(ThisMonth) + "/" + CStr(TodaysDate) + ".txt"
 
Dim myFileStream As New System.IO.FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
 
'Create the stream writer
Dim myWriter As New System.IO.StreamWriter(myFileStream)
 
'Write in what is in the text box
myWriter.WriteLine(CStr(DailyNonRegTimeValue))
 
'Flush before we close
myWriter.Flush()
 
'Close everything
myWriter.Close()
myFileStream.Close()
 
haveExclusiveAccess = True
 
Catch ex As Exception
 
MsgBox("1212 " + ex.ToString)
haveExclusiveAccess = False
 
End Try
 
End While
 
Return 1
 
End Function
 
Last edited:

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
First up, please use the button provided in the advanced editor to wrap your code snippets in formatting tags. It's very hard to read otherwise and the more difficult you make it for us to help, the less likely we will.

As for your code, it's way more complicated than it needs to be. There's really no reason, in this case, to create a FileStream first. Just create the StreamReader or StreamWriter directly. Also, a Using statement will ensure that the file is closed, e.g.
VB.NET:
Using reader As New StreamReader("file path here")
    'Use reader here.
End Using
There's no need to flush or close. Even simpler, you can use methods of the File class like ReadAllText, ReadAllLines, WriteAllText and AppendAllText. the file is opened and closed internally, so you can read and write text in a file in one line of code.
 

brandonyoung

Member
Joined
Apr 26, 2011
Messages
12
Programming Experience
3-5
Reply

Thankyou for the reply.

I will go ahead and edit the code above after I submit this.

With regards to the code, I was doing just as you advised, using streamreader. But thats when I was experiencing the problem most. Randomly I wouldnt be able to write to the file. It would say that the file was being used by another process. Hence why I ended up going with the fileshare.readwrite. It appears to be working fine this way but I would just like and understanding as to why it would randomly just lock up? Do you have any ideas?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Your code is not really any more readable because there's no indenting.

Making your code more complicated is not the answer. Use a Using block and you know for a fact that the file will be closed.
 

brandonyoung

Member
Joined
Apr 26, 2011
Messages
12
Programming Experience
3-5
Reply

I agree with you that making it more complicated is bad way to go about things but this above code seems to do a better job than using a block. Its as if the file would just not get unlocked. Strange because the same function would work over and over and then just throw an exception.

thanks for your time
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Unless there is something corrupt on the system, a using block will absolutely, positively close the file at the end of the block. There must have been something else at play, e.g. overlapping Using blocks in multiple threads. Unless we know more about the specific circumstances, we can really only guess.

Your original code still lacks indenting so is still hard to read.
 
Top Bottom