我正在开发一个 VOIP 客户端(Windows 形式),当调用或接听电话时,它会打开一个浏览器窗口 (ExtendedWebBrowser)。通常情况下它工作正常,但如果计算机进入休眠模式然后唤醒,程序将崩溃并出现以下错误:
"System.Threading.ThreadStateException crossed a native/managed boundary
Message=ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment."
我尝试使用 thread.SetApartmentState(ApartmentState.STA)
设置在代码中启动的线程,但得到了相同的结果。另外,我在 main()
函数之前确实有 [STAThread]
。现在我想知道这是否与计算机进入休眠状态时线程未正确终止有更多关系,但我不确定。我的背景是 C++ 和 DirectX,这是我开发的第一个 Windows 窗体,因此我们将不胜感激。
后续:在发现恢复程序后是在工作线程而不是主线程上启动后,我能够解决此问题。更多信息可以在这里阅读:Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread
最佳答案
我能够解决这个问题。当程序从 sleep 模式恢复并调用 run()
时,它正在 Windows 事件工作线程而不是主线程上运行。更多信息可以在这里阅读:Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread
关于ActiveX 控件无法实例化,因为线程位于单线程单元中。将电脑从 sleep 状态唤醒后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10520872/