为什么的WinForms应用STAThread默认?WinForms、STAThread

2023-09-04 00:38:18 作者:女汉子输给了萌妹子

当你创建一个空的的WinForms 应用程序与Visual Studio中,模板在主应用程序类的 STAThread 属性。

我一直在阅读关于它的一些文档,但我不知道如果我的理解它。

我真的有一些关于它的问题:

为什么这个属性加? 这是什么意思? 如果您删除这个属性会发生什么? 解决方案   

1。为什么这个属性加?

由于它需要由ActiveX对象模型。你可以在一个WinForm下降ActiveX控件(所以它是有兼容性)或某些.NET类使用的要求属性本地控制。

  

2。这是什么意思?

这意味着线程在单线程单元模型运行。

  

3。如果你删除这个属性会发生什么?

如何使用爱剪辑的默认工程文件设置

如果该属性被删除,该行为是不确定的。该程序可能会随机出现故障,有时用有意义的错误消息。举例来说,事情可能现在的工作,然后用一个服务包破。

When you create an empty WinForms application with Visual Studio, the template has the STAThread attribute in the main application class.

I have been reading some docs about it, but I'm not sure if I understood it at all.

Really I have some questions about it:

Why is this attribute added? What does it mean? What happens if you remove this attribute?

解决方案

1. Why is this attribute added?

Because it is required by the ActiveX object model. And you can drop ActiveX controls on a WinForm (so it is there for compatibility) OR some .NET classes use native controls which require that attribute.

2. What does it mean?

It means the thread runs in the single-threaded apartment model.

3. What happens if you remove this attribute?

If the attribute is removed, the behavior is undefined. The program may fail at random, with sometimes sensible error messages. For example, things may work now, then break with a service pack.