当前线程必须在 VB.NET 中设置为单线程单元

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14110565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:43:34  来源:igfitidea点击:

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

.netvb.net

提问by Saeed-rz

This is the newmethod in my form:

这是new我的表单中的方法:

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 InitializeComponentcommand gets an error.

当我创建表单的新对象时,该InitializeComponent命令出错。

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.

在进行 OLE 调用之前,必须将当前线程设置为单线程单元 (STA) 模式。确保您的 Main 函数上标记了 STAThreadAttribute。

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?

此表单在一个项目中,其输出是另一个项目的 DLL 文件,并且该错误不会出现在另一个使用此 DLL 文件的项目中。我该如何解决?

回答by M_Mogharrabi

I have used the following code from this siteand 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;
    }

回答by Hans Passant

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.

不要忽略 TrySetApartmentState() 的返回值。如果您得到 False,则没有理由尝试继续,您的代码将无法运行。你还不如抛出一个异常。

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.

当您尝试从控制台模式应用程序或从不是 Winforms 或 WPF 应用程序的主线程的线程中使用您的代码时,您将收到此异常。对于用户界面组件而言,这些环境并不友好。

A thread is required that entered the STA apartment beforeit 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.

线程需要在启动之前进入 STA 单元,通过应用程序 Main 方法上的 [STAThread] 属性或通过在启动线程之前调用 Thread.SetApartmentState()。并且必须已调用 Application.Run() 或 Form.ShowDialog() 以获得保持表单功能所需的消息循环。通过查看调用堆栈来调试它以查看您的构造函数是如何被调用的。使用 Debug + Windows + Threads 有助于查看这是否发生在工作线程而不是应用程序的主线程上。