Question dispose a webbrowser

Zexor

Well-known member
Joined
Nov 28, 2008
Messages
520
Programming Experience
3-5
how do i dispose of a runtime created webbrowser? The browser isnt display on the form. I also prevent new windows from opening.

VB.NET:
Dim browser as New WebBrowser
AddHandler browser.NewWindow, AddressOf Browser_NewWindow
.
.
.
browser=nothing

Private Sub Browser_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        e.Cancel = True
End Sub

most of the time this seem to work. But something i still hear ads running with video in the back.
 
Is there a different between setting it to nothing and dispose?

They bear no resemblance. I haven't read Ian's link and it may well provide this information but you cannot set a WebBrowser, or any object for that matter, to Nothing. An object is something and something inherently cannot be nothing. You need to understand the difference between an object and a variable. You don't declare an object, you declare a variable. You then create an object and assign it to the variable. If you set the variable to Nothing then the variable no longer refers to the object but the object still exists. If you are married and get a divorce then you have just set your spouse variable to Nothing but that doesn't mean that the person who was your spouse ceases to exist. So, if you set your WebBrowser variable to nothing then it doesn't refer to your WebBrowser object any more but the object still exists and, if you haven't disposed it, still retains all its resources.

In short, if you no longer need to use an object and that object has a Dispose method, call it. If that object is assigned to a local variable then there's most likely no need to set the variable to Nothing because the variable ceases to exist when it falls out of scope, so the reference is removed anyway. Only if the variable stays in scope for some time after you dispose the object do you need to set it to Nothing. By doing so, the reference to the object is removed and, if that's the last reference, the object then becomes eligible for garbage collection.
 
Back
Top