当前位置:首页 > 开发 > 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# 可空参数

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

C# [OnPaint]和[OnPaintBackground]的区别

OnPaint和OnPaintBackground的主要功能区别OnPaint:OnPaint方法主...

C# 类接口

定义接口是一种抽象类型,它定义了一组方法签名(方法名称、参数列表和返回类型),但没有方法体。接口用于...

C# OnMeasureItem

1. **整体功能概述**   - `OnMeasureItem` 是一个在Wi...

C# MemoryStream转为Image

        //...