Skip to content

File locked Exception handling

Martin H

Asked Aug 10, 2010, 8:05 AM EDT1 answer
Share
LinkedInX
I have 2 applications:
The first displays a data group and pulls if from multiple locations to a central location.  As it does so it formats the data and updates a txt file.

The second is used in multiple locations and reads from that location then displays customized views from there.  When making these 2 applications I was lazy and put very little error handling in them.

My major concern id when the First app is building the files on random occasions one of the instances of the 2nd app will try and access the txt file and hit a debug/crash with "File in use" .

My question:
Is there a generic way to handle the exception and just retry again on the next interval timer?

Here is what I am trying now -

Try
        Dim i As Integer

 For i = 1 To 5

                Dim sr As New IO.StreamReader("C:\Program Files\Snapshot\bin\StatusData_" & i & ".txt")
                Dim tempArray()
                Dim r As Integer
                r = 1

                Do While Not sr.EndOfStream
                    tempArray = Split(sr.ReadLine, ",")
                    If tempArray(0) = "91" Then
                        If tempArray(5) = "5" Then
                            TextBox1.AppendText(Environment.NewLine & tempArray(1))
                            RedCount = r + 1
                        End If

                Loop
                sr.Close()
            Next
        Catch de As Exception

        End Try

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    My solution works above but I am not sure if it is continuing in the code.  Played around with it and I am getting mixed results.  Does it stop at the Catch? or does it continue flowing thru the code?

    It does! I had a weak minded moment, but here is a test sub I made using a list box and Combo box.  With this I can lock the file (using Excel as it hates to share files while it is editing them) I then opened the application then opened and closed the csv file and manipulated the combo box to see if the code still continues.

        Private Sub ComboBox2_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedValueChanged
            GetCountandData()
        End Sub

        Private Sub GetCountandData()
            ListBox1.Items.Clear()
            Try
                Dim sr As New IO.StreamReader("C:\Program Files\LAL\charlist.csv")
                Dim tempArray()
                Do While Not sr.EndOfStream
                    tempArray = Split(sr.ReadLine, ",")
                    If tempArray(1) = ComboBox2.Text Then
                        ListBox1.Items.Add(tempArray(0))
                    End If
                    If ComboBox2.Text = "All" Then
                        ListBox1.Items.Add(tempArray(0))
                    End If

                Loop
                sr.Close()

            Catch de As Exception
                ListBox1.Items.Add("ITS BROKE")

            End Try
        End Sub
    0