当前位置:首页 > 开发 > 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# string与Hex互转

StrToHex    /// <summary>...

C# 可空参数

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

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

static void Main() { Thread thre...

C# BackgroundWorker的例子

以下是一个使用 BackgroundWorker 组件在 C# 中实现后台执行任务,同时在主线程更新...

C# System.IO.Path

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

C# ref 和out

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