I wonder if anyone can help.
I am upgrading a Windows Application from Visual Studio 2003 to 2005. After running the conversion wizard and changing/rewriting several methods I got the app to compile (already works perfectly in VS 2003)
However when running the project with a Debugger attached in 2005 and I select the open file dialog box which executes the below code I get the error "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."
The stack trace is shown asat System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at Pupil_Accounts_Final_App.frmmain.importbob() in C:\Documents and Settings\*****\My Documents\Visual Studio 2005\*****Accounts App V2 database upgrade\frmmain.vb:line 379
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
So I then went into my puplicMain class which invokes the form (frmmain) where the button is located for calling the code to start the threaded process to call the method to open the dialog and added the SingleThreadAttribute as shown in the main code below, however the same error is still thrown. I then added this attribute to the button click event and the method (again shown below) but again same problem again.
I have been searching about the Web for several hours trying to find a solution however found one similar article which suggested removing all old DLL files in the BIN directory, I tried this still no luck. Does anyone have any idea what my problem could be? MSDN and all my other usual methods of debugging are just not working with this...
Method for open dialog:
STAThreadAttribute() _<br /> Private Sub importpupils_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimportbob.Click<br />
Main entry point to the program publicMain
Module publicmain<br /> Public Const appversion As String = "2.0.0"<br /> Public Const appauthor As String = "*****"<br /> Public Const apptitle As String = "*******"<br /> Public MainForm As frmmain<br /> Public importPupilsThread As Boolean = False<br /> Public importSeemisThread As Boolean = False<br /> Public nextThread As Boolean = False<br /><br /><br /> ' Main entry point in to application<br /> ' Creates new instance of the main form then shows the dialog<br /> <STAThreadAttribute()> _<br /> Sub Main()<br /> MainForm = New frmmain()<br /> MainForm.ShowDialog()<br /> End Sub