STAThread() 错误的 vb.net 问题:invalidOperationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18242819/
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 14:40:19  来源:igfitidea点击:

vb.net problems with STAThread() Error: invalidOperationException

vb.netsta

提问by MaBi

My Problemis:

我的问题是:

I want to make the function "createFolder" faster (or at least not blocking my main thread), by adding a new callback to my threadpool. The result is that.

我想让函数“createFolder”更快(或者至少不阻塞我的主线程),通过向我的线程池添加一个新的回调。结果

I marked the main function with the STAThread() and the exception tells me to mark my main function with the STAThread().

我用 STAThread() 标记了 main 函数,异常告诉我用 STAThread() 标记我的 main 函数。

I'm open to any tips!

我愿意接受任何提示!

回答by competent_tech

You have placed the STAThreadattribute on the wrong method: it needs to be the method that the form is started from, not the method you are executing.

您将STAThread属性放在错误的方法上:它需要是表单启动的方法,而不是您正在执行的方法。

In many cases, your application will have a Sub Mainand this is what needs to be decorated with the STAThreadattribute. An example from MSDN:

在许多情况下,您的应用程序将有一个Sub Main,这就是需要使用该STAThread属性进行修饰的内容。来自 MSDN 的一个例子:

Public Class MyForm
   Inherits Form

   Public Sub New()
      Me.Text = "Hello World!" 
   End Sub 'New 

   <STAThread()> _
   Public Shared Sub Main()
      Dim aform As New MyForm()
      Application.Run(aform)
   End Sub 

End Class