从工作线程中,我调用 UI 线程,如下所示:
using(CefGlueBrowserForm cefGlueBrowserForm = new CefGlueBrowserForm(propertyBag.ResponseUri.ToString()))
{
CefGlueBrowserForm cefGlueBrowserForm = new CefGlueBrowserForm(propertyBag.ResponseUri.ToString());
cefGlueBrowserForm.Show();
while (!cefGlueBrowserForm.Done)
{
Application.DoEvents();
}
propertyBag.GetResponse = () => new MemoryStream(Encoding.UTF8.GetBytes(cefGlueBrowserForm.DocumentDomHtml));
base.Process(name, propertyBag);
}
这段代码在线程内部,但我总是得到:
System.ComponentModel.InvalidAsynchronousStateException was unhandled
HResult=-2147024809
Message=An error occurred invoking the method. The destination thread no longer exists.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle)
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at Xilium.CefGlue.WindowsForms.CefWebBrowser.InvokeIfRequired(Action a)
at Xilium.CefGlue.WindowsForms.CefWebLoadHandler.OnLoadStart(CefBrowser browser, CefFrame frame)
at Xilium.CefGlue.CefLoadHandler.on_load_start(cef_load_handler_t* self, cef_browser_t* browser, cef_frame_t* frame)
InnerException:
错误在这里:
internal void InvokeIfRequired(Action a)
{
if (InvokeRequired)
Invoke(a); --> ERROR
else
a();
}
它工作了两次(第一个和第二个浏览器)然后崩溃,因为第一个线程关闭了浏览器,并且 2 或 3 个浏览器停止工作。
编辑:
加载浏览器中的页面后,我正在调用:
private void OnLoadEnd()
{
CefGlueBrowser.LoadEnd += (s, e) =>
{
MyCefStringVisitor visitor = new MyCefStringVisitor(this, m_url);
CefGlueBrowser.Browser.GetMainFrame().GetSource(visitor);
};
}
我该如何解决这个问题?我想要多线程 UI 浏览器...
最佳答案
确实没有办法彻底解决这个问题,因为这是一个竞争条件。后台线程无法保证前台线程在整个Invoke
进程中都存在。它可能在 Invoke
开始执行时存在,但在回调实际运行之前被终止。这只是您必须在调用 Invoke
关于c# - UI : An error occurred invoking the method. 目标线程不再存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684366/