c# - 为什么 WinForms 应用程序默认是 STAThread?

标签 c# .net winforms sta

当您使用 Visual Studio 创建一个空的 WinForms 应用程序时,该模板在主应用程序类中具有 STAThread 属性。

我一直在阅读一些关于它的文档,但我不确定我是否完全理解它。

我真的有一些问题:

  1. 为什么添加这个属性?
  2. 这是什么意思?
  3. 如果删除此属性会怎样?

最佳答案

引自an MSDN blog ,

When the STAThreadAttribute is applied, it changes the apartment state of the current thread to be single threaded. Without getting into a huge discussion about COM and threading, this attribute ensures the communication mechanism between the current thread and other threads that may want to talk to it via COM. When you're using Windows Forms, depending on the feature you're using, it may be using COM interop in order to communicate with operating system components. Good examples of this are the Clipboard and the File Dialogs.

关于c# - 为什么 WinForms 应用程序默认是 STAThread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/102437/

相关文章:

c# - 如何在javascript中显示消息框

c# - 将对象转换为多维字符串数组

c# - 比较线程是否相等

c# - 如何控制MDI窗体中子窗体的显示顺序

winforms - 文本框与标签

c# - 无法通过一个查询使用 Include 或 ThenInclude 或 Select/Many 加载相关数据

c# - 字典中的空键条目

c# - .NET 运行时错误导致进程崩溃!但是没有未处理的异常 - 为什么? CLR 错误?

.net - 在 Visual Studio 测试资源管理器中分组组织单元测试

c# - 使用按钮在两个类之间共享方法