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 /> 'starts threading to make program appear faster and optomise performance<br /> Dim t As New Thread(AddressOf importbob)<br /> t.Name = "Import Bob"<br /> importPupilsThread = True<br /> t.Start()<br /> End Sub<br /><br /> <STAThreadAttribute()> _<br /> Public Sub importbob()<br /> 'this sub open's a stream reader so bob's master file can be taken into the program<br /> Dim file As FileStream<br /> Dim inputstream As StreamReader<br /> Dim temp As String<br /> Dim item As ListViewItem<br /> Dim i As Integer<br /><br /> open.Filter = "CSV File (*.csv)|*.csv"<br /> If open.ShowDialog = DialogResult.OK Then<br /> Try<br /> file = New FileStream(open.FileName, FileMode.Open)<br /> inputstream = New StreamReader(file)<br /> Catch ex As Exception<br /> MsgBox("A Exception has occured." + vbCrLf + ex.Message)<br /> Exit Sub<br /> End Try<br /> btnimportbob.Enabled = False<br /><br /> Try<br /> Do While inputstream.Peek > -1<br /> temp = inputstream.ReadLine<br /> item = bobslist.Items.Add(temp.Split(",", 11, StringSplitOptions.None)(0))<br /> For i = 1 To 10<br /> item.SubItems.Add(temp.Split(",", 11, StringSplitOptions.None)(i).Trim)<br /> Next<br /> Loop<br /> Catch ex As Exception<br /> MsgBox("A exception has occured. You may have imported the wrong file or the file is corupt" + vbCrLf + ex.Message)<br /> inputstream.Close()<br /> importPupilsThread = False<br /> Exit Sub<br /><br /> End Try<br /> inputstream.Close()<br /> End If<br /><br /> importPupilsThread = False<br /> btnimportseemis.Enabled = True<br /> End Sub
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