当前位置:首页 > 开发 > 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://www.pythonopen.com/?id=254

相关文章

C# 数据类型

Type ByteLenghtMinMax.NET Framework Typedefau...

DotfuscatorPro使用教程

DotfuscatorPro使用教程

1首次使用,添加反编译工具路径ILASM_v4.0.30319C:\Windows\Microsof...

C# double转为string并保留两位小数

在 C# 中,可以使用多种方式将 double 类型的数据转换为 string 类型并保留两位小数,...

C# decimal

概述在 C# 中,decimal是一种数据类型,用于表示高精度的十进制数值。它主要用于需要精确计算的...

C# MemoryStream转为Image

        //...

C# 控件判断鼠标位置

        //...