3

I am working on a VOIP client (Window's form) that opens a browser window (ExtendedWebBrowser) when a call is made or received. Normally it works fine but if the computer is put into sleep mode and then woken the program will crash 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 have tried setting threads started in the code with thread.SetApartmentState(ApartmentState.STA) but I am getting the same result. Also, I do have [STAThread] before the main() function. Now I am wondering if it has more to do with a thread not terminating correctly when the computer is put to sleep but I'm not sure. My background is in C++ and DirectX, this is the first Windows form I've worked on so any help would be appreciated.

Followup: I was able to resolve this issue after discovering that after resuming the program was being started on a worker thread instead of the main thread. More information can be read here: Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread

Community
  • 1
  • 1
madeFromCode
  • 721
  • 7
  • 15
  • This is typically induced by a problem with the startup of your program. Remove any custom splashscreen you made and make sure the very first window that is created is initialized on the main thread. This ensures that the SystemEvents class is properly initialized. – Hans Passant May 10 '12 at 03:17
  • Hi Hans, thank you for the reply. The first window created is initialized on the main thread. However, the first/main form is not the one that errors. The browser window that is opened by the main form is the one that is giving me the error. Additionally, I believe it's important to note that the error only happens after the computer is put into sleep mode and then woken. OnPowerChange the forms are closed and on resume the forms run again like they do on the initial load. – madeFromCode May 14 '12 at 22:45
  • Well, unclear but a browser would certainly generate this kind of error. Note how your question title is wrong, it complains because it is **not** being created on an STA thread. No idea what OnPowerChange might be but indications are that whatever event you use is running on the wrong thread and thus triggering the complaint. – Hans Passant May 14 '12 at 23:33
  • Sorry, OnPowerChange is the method we have subscribed to the PowerModeChanged event. The title is based off the error I get when the program crashes. After further debugging I found that it only happens after the program is slept and resumed. I have also found that when the program resumes it is running in a worker thread rather than the main thread. I'll be working with this knowledge today to hopefully find a fix. At the time of posting, I didn't know that. I took this project over from another developer so I'm still learning how it works. Thank you for your help and sorry for the confusion. – madeFromCode May 15 '12 at 15:32

1 Answers1

3

I was able to resolve this issue. When the program resumed from sleep mode and run() was called it was being run on the windows event worker thread instead of the main thread. More information can be read here: Sleep(suspend) and Resuming windows form starts program on worker thread instead of main thread

Community
  • 1
  • 1
madeFromCode
  • 721
  • 7
  • 15