I have a WinForms VB app, using Visual Studio 2010. I am using the WPF DocumentViewer, so it is hosted by the ElementHost control. In my AppMain_Form.Designer.vb, when the follopwing (generated) statement is executed:
Me.rv_doc_vwr_host = New System.Windows.Forms.Integration.ElementHost()
I get an exception (which I had to enable CLR exceptions for the debugger to get):
"The calling thread must be STA, because many UI components require this."
However, in my research for this problem, I found the following:
1) Windows Forms applications are SingleThreaded by default.
2) The Main procedure is generated automatically for Windows Forms applications, and it is hidden.
3) A Main procedure can be manually coded, but this requires disabling the Application Framework, which I believe is rather necessary for a Windows Forms application.
So, why is this exception occurring if the Windows Forms application is SingleThreaded by default? If not, how can I mark the Main procedure as STAThread if it is hidden? Why did I have to enable CLR exceptions to catch this? Prior to enabling CLR exceptions, this did not show up in my testing, but when I ran my app outside of Visual Studio, it displayed an exception box briefly about creating the main form, but before I could get any details it was replaced by my splash screen, then my main form. The app worked normally after that. However, I cannot deploy it with an exception message being displayed every time it starts up.