Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current thread must be set to single thread apartment in VB.NET

Tags:

.net

vb.net

This is the new method in my form:

Public Sub New(ByVal ConnectionString As String, ByVal ConSql As SqlClient.SqlConnection, ByVal Daman As Array, ByVal SDate As Integer, ByVal FDate As Integer)

    Threading.Thread.CurrentThread.TrySetApartmentState(Threading.ApartmentState.STA)
    ' This call is required by the Windows Form Designer.
    'Error Appear in this line
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Me.Sto_TypeFormFrameTableAdapter.Connection.ConnectionString = ConnectionString
    Me.Sto_typeformTableAdapter.Connection.ConnectionString = ConnectionString
    con = ConSql
    com.Connection = con
    ConNew = ConnectionString
    DamaneCod = Daman
    Start = SDate
    Final = FDate
    Fill()
End Sub

When I create a new object of my form, the InitializeComponent command gets an error.

The error message is:

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 form is in a project whose output is a DLL file for another project, and the error does not appear in another project that used this DLL file. How can I fix it?

like image 830
Saeed-rz Avatar asked Jan 01 '13 12:01

Saeed-rz


2 Answers

I have used the following code from this site and it works:

    using System.Threading;

    protected void Button1_Click(object sender, EventArgs e)
    {

       Thread newThread = new Thread(new ThreadStart(ThreadMethod));
       newThread.SetApartmentState(ApartmentState.STA);
       newThread.Start();     

    }

    static void ThreadMethod()
    {
       Write your code here;
    }
like image 170
M_Mogharrabi Avatar answered Nov 09 '22 04:11

M_Mogharrabi


Do not ignore the return value of TrySetApartmentState(). If you get False then there is no reason to try to continue, your code is not going to work. You might as well throw an exception.

If Not Threading.Thread.CurrentThread.TrySetApartmentState(Threading.ApartmentState.STA) Then
    Throw New InvalidOperationException("This form is only usable from the UI thread")
End If

You will get this exception when you try to use your code from a console mode app or from a thread that is not the main thread of a Winforms or WPF application. Those are not hospitable environments for a user interface component.

A thread is required that entered the STA apartment before it got started, either by the [STAThread] attribute on the Main method of an application or by calling Thread.SetApartmentState() before starting the thread. And Application.Run() or Form.ShowDialog() must have been called to get the required message loop that keeps the form functional. Debug this by looking at the call stack to see how your constructor got called. Using Debug + Windows + Threads is helpful to see if this happened on a worker thread instead of the app's main thread.

like image 7
Hans Passant Avatar answered Nov 09 '22 05:11

Hans Passant