Disposing Browser instance


When you don't need to use Browser instance you must dispose it using Browser.Dispose() method. For example:

C#

browser.Dispose();

VB.NET

browser.Dispose()


Disposing BrowserView instance


It is necessary to disposeBrowserView in your code if that BrowserViewwas created via its non-default constructor.


For theBrowserView, you need to dispose the view first and its browser second:

C#

browserView.Dispose();
browserView.Browser.Dispose();

VB.NET

browserView.Dispose()
browserView.Browser.Dispose()


The usual place to disposeBrowserView isFormClosing event for WinForms andWindow.Closing event for WPF.


If the BrowserView is instantiated using the default constructor, it creates a Browser instance inside itself. In this case, theBrowserView is considered as the owner of this browser instance, and that Browser instance will be disposed with the view when the BrowserView.Dispose() is called for it.


In addition, such view will be disposed automatically when the form or window that contains it is closed - this behavior is designed to simplify proper shutdown process for the case when the BrowserView is used as a regular control.


Disposing BrowserContext instance


If you have manually created the BrowserContext instance, then it is recommended to dispose it using the BrowserContext.Dispose() method when you no longer need it. This should be done after all the Browser and BrowserView instances using this context were disposed. For example:

C#

browser.Dispose();
browser.Context.Dispose();

VB.NET

browser.Dispose()
browser.Context.Dispose()


Accessing disposed instance


Once you disposed the Browser instance, you cannot use it anymore. If you try to access already disposed Browser instance theObjectDisposedException exception will be thrown. For example:

C#

browser.Dispose();
var title = browser.Title; // ObjectDisposedException will be thrown

VB.NET

browser.Dispose()
Dim title = browser.Title ' ObjectDisposedException will be thrown


To check if Browser instance is disposed or not, you can use Browser.IsDisposed() method.


Dispose Events


Each Browser instance can be also disposed from JavaScript via window.close() function. In this case, you might be interested in receiving notification when Browser instance is disposed. To get such notifications, you can use event handler. For example:

C#

browser.DisposeEvent += delegate(object sender, DisposeEventArgs e)
{
    // Browser is disposed
};

VB.NET

AddHandler browser.DisposeEvent, Sub(sender As Object, e As DisposeEventArgs)
    ' Browser is disposed
End Sub


When you dispose Browser instance manually via Browser.Dispose() method, the Dispose event will also be fired.