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
THEN THIS FUNCTION IS CALLED
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: