当前位置:首页 > 编程语言 > C# > 正文内容

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

C#2年前 (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();




相关文章

C# CRC32算法

CRC32      class CRC32...

C# 结构体struct 例子

/// <summary> /// 结构体 /// &...

C# 获取带有焦点的控件

this.ActiveControl...

C# MDI例子

父窗口属性IsMdiContainer设置为Trueprivate void b...

C# XML

创建    XmlDocument xmlDoc ...

C#一些重写

        pr...