The decompiler shows that the Dispose
method called at the end of the Using
-statement is implemented like this:
Public Sub Dispose() Implements IDisposable.Dispose
Me.Close()
End Sub
It is implemented by the base class System.IO.Stream
.
However, since you are calling Environment.Exit(-1)
the code is aborted and the Dispose
method is NOT called! You can test it with this class:
Class Disp
Implements IDisposable
Public Sub Dispose() Implements IDisposable.Dispose
MsgBox("Disposing")
End Sub
End Class
and this code:
Private Sub btnTest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTest.Click
Dim x = 100, y = 0, z = 0
Using d = New Disp()
Try
z = x \ y
Catch ex As Exception
Environment.Exit(-1)
End Try
End Using
End Sub
It should display "Disposing" in a message box, but doesn't! If you comment out Environment.Exit(-1)
the message is displayed.
The Using
statement is implemented as a Try Finally
statement, so your code is equivalent to two nested Try ... End Try
blocks. Microsoft's documentation for the Environment.Exit Method says: "... If Exit is called from a try or finally block, the code in any catch block does not execute. ...". But the documentation is unclear on whether the finally block is executed or not. The documentation for Try...Catch...Finally Statement, however, says:
Control does not pass from a Try or Catch block to the corresponding Finally block in the following cases:
•An End Statement is encountered in the Try or Catch block.
•A StackOverflowException is thrown in the Try or Catch block.
And the documentation for the End Statement says: "The End statement calls the Exit method of the Environment class in the System namespace. ...".
Finally, putting these informations together, we can say:
Yes you must call xmlstream.Close()
before calling Environment.Exit(-1)
!
See also: Calling Environment.Exit() Within a Using Block