We've been getting this error a lot lately, and it causes our application to halt. We can't seem to trap it or prevent it:
System.Exception
HResult=0x80131500
Message=Destroy must be called from the same thread the WebView is created.
Source=EO.WebBrowser
StackTrace:
at EO.WebBrowser.WebView.g(Boolean A_0)
at EO.Wpf.WebViewHost.a.a(Boolean A_0)
at System.Windows.Interop.HwndHost.Finalize()
Previously we were using version 17.3.13.0 and never got this error.
This started occurring after we moved to 18.1.75.0
Is there a way to catch this exception or prevent it being thrown? It's a major issue for us.
Edited to Add:
Obviously, we are currently creating and destroying the WebView on the same thread. Or at least we think we are! We've never had any trouble before with it.
Edited to also add:
We have worked out a workaround for the moment, but in so doing we may have discovered a useful way for you to reproduce this issue.
Our situation is that we have a WPF application with a TabControl on it. The TabControl is set up with a webview thus:
Code: C#
// this is all inside a class
private WebView mActiveView;
private bool _loadImages = false;
private bool _enableJavaScript = true;
private void AddBrowserTab(WebView poNewWebView)
{
var loNewTab = new TabItem {Header = "New"};
tabBrowser.Items.Add(loNewTab);
var loWebControl = new EO.Wpf.WebControl {WebView = poNewWebView};
mActiveView = poNewWebView;
mActiveView.SetOptions(new BrowserOptions { LoadImages = _loadImages, AllowJavaScript = _enableJavaScript });
mActiveView.BeforeDownload += wb_BeforeDownload;
// snipped various event handlers
mActiveView.ObjectForScripting = new BrowserListener(ActiveSession);
loNewTab.Content = loWebControl;
tabBrowser.SelectedIndex = tabBrowser.Items.Count - 1;
}
Then, when _loadImages or _enableJavascript is changed, we had other code like this. I have commented on one line, if we remove this line the issue goes away, but the tabcontrol still has the tab on it, just empty. It seems that remvoing the tabcontrol EVEN AFTER Destroying the WebView, throws the exception about about Destroying something on another thread. Our assumption was that the WebView was already destroyed, but apparently not:
Code: C#
public void SetOptions(bool loadImages, bool allowJavaScript)
{
if (loadImages == _loadImages && allowJavaScript == _enableJavaScript) return; // Already set correctly
_loadImages = loadImages;
_enableJavaScript = allowJavaScript;
try
{
mActiveView?.Destroy();
tabBrowser.Items.Remove(tabBrowser.Items[tabBrowser.Items.Count - 1]); // If we remove this line, then everything works
AddBrowserTab(GetNewWebViewWithPreloadedJavascript());
}
catch (Exception ex)
{
// Log stuff.
}
}