r/visualbasic • • May 23 '12

Stubborn StreamReader

Hey guys,

I've got a bit of trouble here, this component of my code writes an integer to a text file on close and then writes it to an integer when the form loads.

The problem I'm having is that the "Process cannot access the file because it is being used by another process" error.

Now I've put it down to my load streamreader, even though I've included the streamreader.close() line it doesn't actually process this line. It only reads the line before it and proceeds to open the form.

Here's my code:


Private Sub Main_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim sw_QuoteNo As StreamWriter
    Dim sFileName As String
    Dim iRecords As Integer

    iRecords = lbxQuoteNumber.Items.Count
    sFileName = Application.StartupPath & "\QuoteEnd.txt"
    sw_QuoteNo = New StreamWriter(sFileName, True)

    If File.Exists(sFileName) Then
        sw_QuoteNo.WriteLine(iQuoteEnd)
    Else
        MsgBox("The file was not found.")
    End If

    sw_QuoteNo.Close()

End Sub

Private Sub frmQuoteSystem_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim sr_QuoteNumber As StreamReader
    Dim sFileName As String
    Dim sLine As String

    sFileName = Application.StartupPath & "\QuoteEnd.txt"
    sr_QuoteNumber = New StreamReader(sFileName, True)

    iQuoteEnd = sr_QuoteNumber.ReadLine

    sr_QuoteNumber.Close()
End Sub


Obviously, one is the form load sub and the other is the closing sub.

Some clarity would be much appreciated, I've been working on this for far too long to no avail.

(I do have other streamreader/writers but they're triggered with a button)

6 Upvotes

4 comments sorted by

View all comments

1

u/Jooniar May 23 '12

Worked it out! If anyone else runs into this issue, I had to use "Using".

e.g.

Using sr_QuoteNumber = New StreamReader(sFileName, True)
iQuoteEnd = sr_QuoteNumber.ReadLine
End Using

Same for my writer.

2

u/-james May 24 '12

I don't like it when things don't work as expected. However, I'm thinking this has to do with lazy Garbage collection. I wonder if you would just explicitly dispose of sw_QuoteNumber in the closing statement if the problem would go away?

Would you mind trying:

    sw_QuoteNo.Close()

    sw_QuoteNo.Dispose()

  End Sub

And put the loaded code back the way you had it?

This should force .Net to dispose of the the writer and release the file handle before exiting the Closing Sub.

Looking forward to your reply.

-james

1

u/Jooniar May 24 '12

Seems to work fine too. Are there any advantages between "Using" and .dispose()?

2

u/-james May 24 '12

I'm about convinced this is a garbage collection bug. Using the dispose() closely describes what you are wanting to accomplish and will make sense to anyone who comes across your code in the future. If MS were to optimize the Using at some point where it doesn't dispose dead variables the same way, the problem could occur again. Does this make sense?

-james

1

u/Jooniar May 24 '12

Yep! And I'll definitely keep it in mind. Chances are I'll run into this again.

Thank you very much for the assistance.