c# - UI : An error occurred invoking the method. 目标线程不再存在

标签 c# multithreading chromium-embedded

从工作线程中,我调用 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/

相关文章:

c# - CefSharp 是多线程的吗?

c++ - CEF 框架在关闭前发送 http 请求

c# - 我可以重构我的 linq select 吗?

c# - Unity 2d 运动脚本问题

c++ - 与普通变量相比,仅读取原子变量是否有性能差异?

java - 并行MapReduce

html - 两个并排的元素,一个未知/可变宽度,另一个填充剩余的水平空间?

c# - 在 C# 中测量执行时间

c# - 为什么当我按下按钮时我的列表会重置?

java - AsyncTask #2 java.lang.RuntimeException