为什么的WinForms应用STAThread默认? [英] Why are WinForms applications STAThread by default?

查看:224
本文介绍了为什么的WinForms应用STAThread默认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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:

  1. Why is this attribute added?
  2. What does it mean?
  3. 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.

这篇关于为什么的WinForms应用STAThread默认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆