General idea about close

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
Visual Basic:
    Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click
        Try
            Dim FilePath As FileStream = Nothing
            For Counter As Integer = 1 To Some Number
                FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)
                ...process
                FilePath.Close()
            Next
        Catch x As x
            ...
        Finally
            FilePath.Close()
        End Try
    End Sub
The above code is general structure of my code, as you can see there, I create and use a file stream and close it before each next loop command.
But it is possible that some time due to an error control goes outside of the loop into the catch section without closing the file stream.
So I put a FilePath.Close() in the finally section to make sure that file is closed.
But if no error occurs, I get another error for FilePath.Close() in the finally section! I cannot close an already closed stream.
What should I do for such case? What do you recommend?
Thanks all:)
 
Visual Basic:
Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click

Try
    Dim FilePath As FileStream = Nothing

    For Counter As Integer = 1 To Some Number
        FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)

         ...process

     Next
Catch x As x
         ...
Finally
      If Not FilePath Is Nothing Then
          FilePath.Close()
      End If
End Try

End Sub

You don't need to close the file within the Try block as the Finally will always run - it is a good idea to check that you have a valid file though just incase the New FileStream(...) bit fails for any reason.
 
Thanks, but for each time that loops runs in For Next, I will need to open a different new file, won't this make problem?:)
 
Closing within loop

Since you're opening and closing the file within a loop, I expect PlausiblyDamp's alteration won't be exactly what you want. If you set FilePath to Nothing after you close it within the loop, the Finally will not attempt to close it.

Visual Basic:
Try
    Dim FilePath As FileStream = Nothing

    For Counter As Integer = 1 To Some Number
        FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)

        '...process

        FilePath.Close()
        FilePath = Nothing
    Next
Catch x As Exception
    '...
Finally
    If FilePath IsNot Nothing Then
        FilePath.Close()
    End If
End Try

Good luck :)
 
Whoops - my bad. You would probably be better of removing the Finally block and closing the file in the Catch block.
Visual Basic:
Catch x As x
         ...
      If Not FilePath Is Nothing Then
          FilePath.Close()
      End If
End Try
 
Since the code is likely a bit larger than your sample, I'd think that moving the code inside of the loop into a separate function. That separate function is responsible for the file creation/writing/closing. In that function you'd have a try/catch/finally. Something like this:
Visual Basic:
Private Sub ProcessButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessButton.Click
	For Counter As Integer = 1 To Some Number
		ProcessFile(FileString)
	Next
End Sub
    
Private Sub ProcessFile(ByRef FileString As String)
	Dim FilePath As FileStream = Nothing

	Try
		FilePath = New FileStream(FileString, FileMode.Create, FileAccess.ReadWrite)
		...process
	Catch x As x
		...
	Finally
		' If file was opened and created, close it
		If (FilePath Is Not Nothing)
			FilePath.Close()
		End If
	End Try
End Sub

I left the check against "Nothing" in the finally since there's a chance that the file opening/creation failed and hit the Catch block. You don't want an exception in the Finally block while trying to call Close on a null reference.

If you need to know if a file failed so that you won't continue looping, you can have ProcessFile return a bool indicating success/error and check that within the loop - or anything else you want.

-ner
 
Back
Top