I am having WebBrowser in my WPF application. I use it for viewing pdf files. But after 50 files are viewed the app throws an exception. So we decided to dispose webbrowser before opening next pdf. The thing is I do not know how exactly to do that. When I dispose webbrowser it is unbinded and no pdf is viewed anymore. It makes sence, but how to do that correctly? Here is my code:
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
try
{
browser.Dispose();
browser = null;
browser = new WebBrowser();
}
catch
{
}
string uri = e.NewValue as string;
try
{
if (uri != null)
{
browser.Navigate(new Uri(uri, UriKind.Absolute));
}
}
catch (Exception exception)
{
ExceptionPolicy.HandleException(exception, ExceptionPolicies.General);
}
}
}
And xaml part:
<WebBrowser Grid.Row="0" Behaviour:WebBrowserBehaviour.BindableSource="{Binding WebBrowserData}"
LoadCompleted="WebBrowser_LoadCompleted" />
Basically the problem sounds like you have a memory leak. To help identify it you should use software designed that that purpose. RedAnt has a nice library that I believe has a free trial you can use to help determine what the exact cause of the issue is.
To dispose after loading you can do what is suggested here: How to dispose of a web browser control after show
But I would suggest you prove that is the problem first by watching the memory or using a tool designed to help detect leaks.