当前位置:首页 > 开发 > C# > 正文内容

C# 请确保您的 Main 函数带有 STAThreadAttribute 标记。”

C#3年前 (2022-12-23)

System.Threading.ThreadStateException:“在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。”


    

在函数头部增加[STAThreadAttribute]。


原代码:

static void Main(string[] args)
{
    System.Windows.Forms.Clipboard.SetText("无尽的华尔兹");
}

修改后:

[STAThreadAttribute]
static void Main(string[] args)
{
    System.Windows.Forms.Clipboard.SetText("无尽的华尔兹");
}




    
using System.Threading;
Thread t = new Thread((ThreadStart)(() =>
{
    //放入异常语句
}));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();




转载请注明出处。

本文链接:http://pythonopen.com/?id=254

相关文章

C# 将函数作为参数传递

无参数    static void Main(s...

C# 可空参数

using System; using System.Runtime.Inte...

C# 缩减代码量的一些方式

static void Main() { Thread thre...

C# System.IO.Path

System.IO.Path.GetExtension返回指定的路径字符串的扩展名。string&n...

C# BackgroundWorker,在DoWork里更新控件内容

一般情况下不可以直接在BackgroundWorker的DoWork事件中更新 UI 控件在Back...

C# ref 和out

ref关键字概念:ref是 C# 中的一个关键字,用于按引用传递参数。当在方法调用中使用ref关键字...