Click here to Skip to main content
Rate this: bad
good
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
        t.Start()
    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
            Try
                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

            Try
                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)
                    Next
                Loop
            Catch ex As Exception
                MsgBox("A exception has occured. You may have imported the wrong file or the file is corupt" + vbCrLf + ex.Message)
                inputstream.Close()
                importPupilsThread = False
                Exit Sub

            End Try
            inputstream.Close()
        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()
        MainForm.ShowDialog()
    End Sub
Posted 28-Jul-09 1:57am

1 solution

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

Solution 1

Hi,

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 | :)

  Permalink  
  Print Answers RSS
0 Sergey Alexandrovich Kryukov 334
1 Kornfeld Eliyahu Peter 325
2 Maciej Los 239
3 OriginalGriff 188
4 King Fisher 185
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Jul 2009
Copyright © CodeProject, 1999-2014
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