An ActiveX control cannot be created because the thread is in a single-threaded apartment. It happens after waking the PC from sleep

I am working on a VOIP client (window form) that opens a browser window (ExtendedWebBrowser) when a call is made or received. It usually works fine, but if the computer goes into sleep mode and then wakes up, the program will exit with the following error:

"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."

I tried setting up the threads running in the code using thread.SetApartmentState(ApartmentState.STA), but I get the same result. In addition, before main()I have [STAThread]. Now I am wondering if this is due to a thread that does not end correctly when the computer goes to sleep, but I'm not sure. My background is in C ++ and DirectX, this is the first form of Windows I worked on, so any help would be appreciated.

Follow-up observation: I was able to solve this problem after discovering that after the program was resumed, a worker thread started instead of the main thread. More information can be found here: Sleep (pause) and resuming window form launches the program on a workflow instead of the main thread

+3
source
1

. run(), Windows . : ()

+3

All Articles