Click here to Skip to main content
12,400,556 members (59,408 online)
Rate this:
Please Sign up or sign in to vote.
See more: .NETCF .NET
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 as

at System.Windows.Forms.FileDialog.RunDialog(IntPtr hWndOwner)
at System.Windows.Forms.CommonDialog.ShowDialog(IWin32Window owner)
at System.Windows.Forms.CommonDialog.ShowDialog()
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)
at System.Threading.ThreadHelper.ThreadStart()

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() _
    Private Sub importpupils_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnimportbob.Click
        'starts threading to make program appear faster and optomise performance
        Dim t As New Thread(AddressOf importbob)
        t.Name = "Import Bob"
        importPupilsThread = True
    End Sub

    <STAThreadAttribute()> _
    Public Sub importbob()
        'this sub open's a stream reader so bob's master file can be taken into the program
        Dim file As FileStream
        Dim inputstream As StreamReader
        Dim temp As String
        Dim item As ListViewItem
        Dim i As Integer

        open.Filter = "CSV File (*.csv)|*.csv"
        If open.ShowDialog = DialogResult.OK Then
                file = New FileStream(open.FileName, FileMode.Open)
                inputstream = New StreamReader(file)
            Catch ex As Exception
                MsgBox("A Exception has occured." + vbCrLf + ex.Message)
                Exit Sub
            End Try
            btnimportbob.Enabled = False

                Do While inputstream.Peek > -1
                    temp = inputstream.ReadLine
                    item = bobslist.Items.Add(temp.Split(",", 11, StringSplitOptions.None)(0))
                    For i = 1 To 10
                        item.SubItems.Add(temp.Split(",", 11, StringSplitOptions.None)(i).Trim)
            Catch ex As Exception
                MsgBox("A exception has occured. You may have imported the wrong file or the file is corupt" + vbCrLf + ex.Message)
                importPupilsThread = False
                Exit Sub

            End Try
        End If

        importPupilsThread = False
        btnimportseemis.Enabled = True
    End Sub

Main entry point to the program publicMain

Module publicmain
    Public Const appversion As String = "2.0.0"
    Public Const appauthor As String = "*****"
    Public Const apptitle As String = "*******"
    Public MainForm As frmmain
    Public importPupilsThread As Boolean = False
    Public importSeemisThread As Boolean = False
    Public nextThread As Boolean = False

    ' Main entry point in to application
    ' Creates new instance of the main form then shows the dialog
    <STAThreadAttribute()> _
    Sub Main()
        MainForm = New frmmain()
    End Sub
Posted 28-Jul-09 0:57am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


I don't think you can use STAThreadAttributes for that purpose. Here are two possible solutions:
1. do you user interaction before you start the thread;
2. or set Thread.ApartmentState before calling Thread.Start

Smile | :)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 28 Jul 2009
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100