Current thread must be set to single thread apartment (STA)

G

Guest

Hi

I have just upgraded a vb.net 2003 project to 2005. I get an error when
trying to access a FolderBrowserDialog as follows

Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

FolderBrowserDialog1.Description = "Please select a location for
your web pages."

'Default to the c drive if it exists
If Directory.Exists("c:\") = True Then
FolderBrowserDialog1.SelectedPath = "c:\"
Else
FolderBrowserDialog1.SelectedPath = ""
End If

FolderBrowserDialog1.ShowDialog()

end sub

The error I get is:
"the 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."

So i created a class 'clsStartup' containing a main procedure:

Public Class clsStartup

<STAThread()> _
Shared Sub Main()

' Declare a variable named frm1 of type Form1.
Dim frm1 As frmWSGWebSiteLocationUpdate

' Instantiate (create) a new Form1 object and assign

' it to variable frm1.

frm1 = New frmWSGWebSiteLocationUpdate()

' Call the Application class' Run method

' passing it the Form1 object created above.

Application.Run(frmWSGWebSiteLocationUpdate)

End Sub

End Class

And set the startup object to be clsStartup.

But I still get the same message.

Can anyone help me please?

Regards,

Steve.
 
G

Guest

Also tried using the <STAThreadAttribute()> with same result.

Public Class clsWSGStartup

<STAThreadAttribute()> _
Shared Sub Main()

' Declare a variable named frm1 of type Form1.
Dim frm1 As frmWSGWebSiteLocationUpdate
frm1 = New frmWSGWebSiteLocationUpdate()
Application.Run(frm1)

End Sub

End Class

Regards,

Steve.
 
G

Guest

I finally found the cause of the problem for anyone who might be interested.

I removed an out of date dll stored in bin\temp directory. THis dll was not
referenced by the project, so I have no idea why it should cause a problem.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top