当前位置: 首页> 科技> 互联网 > discuzq_中国纪检监察报记者电话_手机优化游戏性能的软件_知乎关键词排名工具

discuzq_中国纪检监察报记者电话_手机优化游戏性能的软件_知乎关键词排名工具

时间:2025/9/5 6:20:49来源:https://blog.csdn.net/chenhao0568/article/details/144526078 浏览次数:0次
discuzq_中国纪检监察报记者电话_手机优化游戏性能的软件_知乎关键词排名工具

在这里插入图片描述

C# DragDrop 注册失败

System.InvalidOperationExceptionHResult=0x80131509Message=DragDrop 注册失败。Source=System.Windows.FormsStackTrace:在 System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)在 System.Windows.Forms.Control.OnHandleCreated(EventArgs e)在 System.Windows.Forms.Form.OnHandleCreated(EventArgs e)在 System.Windows.Forms.Control.WmCreate(Message& m)在 System.Windows.Forms.Control.WndProc(Message& m)在 System.Windows.Forms.Form.WmCreate(Message& m)在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)此异常最初是在此调用堆栈中引发的: System.Windows.Forms.Control.SetAcceptDrops(bool)内部异常 1:
ThreadStateException: 在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式。请确保您的 Main 函数带有 STAThreadAttribute 标记。

在这里插入图片描述
查看状态,看是从哪里由STA变成了MTA

Thread.CurrentThread.GetApartmentState()

修改后
在这里插入图片描述

static async Task Main(string[] args) 的原因是因为 async Task Main 的默认线程模式是多线程单元(MTA),而 DragDrop 功能需要单线程单元(STA)。async Task Main 是 C# 支持异步主方法的新特性,但它不能直接与 [STAThread] 一起使用。

以下是解决这个问题的几种方法:


解决方法 1: 使用同步 Main 方法作为入口点

Main 方法改为同步方法,同时在同步方法中调用异步方法并等待完成。

修改代码:
[STAThread]
static void Main(string[] args)
{MainAsync(args).GetAwaiter().GetResult();
}static async Task MainAsync(string[] args)
{// 异步逻辑await Task.Delay(1000);Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MainForm());
}

这种方法保留了异步逻辑的灵活性,同时确保线程是 STA 模式。


解决方法 2: 创建 STA 线程运行 Application.Run

如果必须保留 async Task Main 作为入口,可以在 Main 方法中创建一个 STA 线程来运行 Application.Run

修改代码:
static async Task Main(string[] args)
{// 异步逻辑await Task.Delay(1000);var staThread = new Thread(() =>{Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MainForm());});staThread.SetApartmentState(ApartmentState.STA); // 设置线程为 STA 模式staThread.Start();staThread.Join(); // 等待线程完成
}

这种方法将所有需要 STA 模式的代码放在单独的线程中执行。


解决方法 3: 弃用 async Task Main,改为异步控制流程

如果 async Task Main 不是必须的,可以直接使用同步代码控制应用的启动过程。

修改代码:
[STAThread]
static void Main(string[] args)
{Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);// 异步调用并等待完成var result = RunAsyncLogic().GetAwaiter().GetResult();Application.Run(new MainForm());
}static async Task<int> RunAsyncLogic()
{await Task.Delay(1000); // 模拟异步操作return 0;
}

这种方式让 Main 保持同步,异步逻辑分离到单独的方法中。


总结

  • 如果需要支持异步逻辑且要保持线程 STA 模式,解决方法 1 是最优选择,兼顾异步灵活性和 DragDrop 的要求。
  • 如果必须使用 async Task Main,可以采用 解决方法 2,在 STA 线程中运行 Windows 窗体或 WPF 相关代码。

C#窗口程序如何看控制台打印的日志

这样输出日志就能看见了。
在这里插入图片描述

 Console.WriteLine($"Main Thread ApartmentState: {Thread.CurrentThread.GetApartmentState()}");

在这里插入图片描述

关键字:discuzq_中国纪检监察报记者电话_手机优化游戏性能的软件_知乎关键词排名工具

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: