无法实例化ActiveX控件,因为线程位于单线程单元中。从睡眠中唤醒PC后发生

时间:2012-05-09 17:11:56

标签: activex sta single-threaded apartments apartment-state

我正在开发一个VOIP客户端(Window的表单),在进行或接收呼叫时打开浏览器窗口(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)设置在代码中启动的线程,但我得到了相同的结果。另外,我在[STAThread]函数之前有main()。现在我想知道当计算机进入睡眠状态时,它是否与线程无法正常终止有关,但我不确定。我的背景是C ++和DirectX,这是我工作的第一个Windows表单,所以任何帮助都会受到赞赏。

后续行动:在发现在工作线程而不是主线程上启动程序后,我能够解决此问题。可在此处阅读更多信息:Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread

1 个答案:

答案 0 :(得分:3)

我能够解决这个问题。当程序从睡眠模式恢复并且run()被调用时,它正在Windows事件工作线程而不是主线程上运行。可在此处阅读更多信息:Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread

相关问题