When debugging in VS2005 I get an exception if I try to open a Dialog Box. first question is why is this WinForms app not STA??
System.Threading.ThreadStateException was unhandled
Message="Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
at TorPlugin.TorPanel.Button3_Click(Object sender, EventArgs e) in C:\Projects\OAMSIMULATOR\Inspector\plugins\TorPlugin\TorPanel.vb:line 132
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at TorPlugin.Form1.Main() in C:\Projects\OAMSIMULATOR\Inspector\plugins\TorPlugin\Form1.vb:line 39
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
?System.Threading.Thread.CurrentThread.GetApartmentState
MTA {1}
<STAThread()> _
Shared Sub Main()Application.Run(New Form1())
End Sub
<STAThread()> _
Shared Sub Main()
Debug.Print("Sub Main is " & System.Threading.Thread.CurrentThread.GetApartmentState.ToString)
Application.Run(New Form1())
End Sub
Sub Main is MTA
A bug
When running under VS2005 in debug mode using ThreadingModel.vshost.exe to launch ThreadingModel.exe, the Threading Model is supposed to be STA. It is STA, provided that there is not a ThreadingModel.dll file in the same directory as ThreadingModel.exe. If a ThreadingModel.dll file exists in the same directory. The Threading Model ends up being MTA.
This was indeed the case. I built to bin\App where there was no TorPlugin.dll and then voila':
Sub Main is STA