C#的关键字 [STAThread]即 single-threaded apartment
一:
每个Thread都有一个关于ApartmentState的属性,可以把它设置为:STA或者MTA,或者UNKNOWN。
当你想指定工程的启动窗口的时候,你需要在该窗口类中申明一个Main()方法,并为这个方法设置[STAThread]属性。
详细信息,清查阅MSDN中关于Threading和COM Interop和COM+ Apartment Model的文章:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconmanagedunmanagedthreading.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconadvancedcominterop.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cossdk/htm/pgservices_synchronization_8703.asp

二:
[STAThread]是Single  Thread  Apartment单线程套间的意思,是一种线程模型,用在程序的入口方法上
(在C#和VB.NET里是Main()方法),来指定当前线程的ApartmentState 是STA。用在其他方法上不产生影响。
在aspx页面上可以使用AspCompat = "true" 来达到同样的效果。这个属性只在  Com  Interop  有用,
如果全部是  managed  code  则无用。简单的说法:[STAThread]指示应用程序的默认线程模型是单线程单元 (STA)。
启动线程模型可设置为单线程单元或多线程单元。如果未对其进行设置,则该线程不被初始化。也就是说如果你用的.NET Framework,
并且没有使用COM Interop,一般不需要这个Attribute。其它的还有MTA(多线程套间)、Free  Thread(自由线程)。
单线程套间,简单来说所有对于单线程套间中对象的访问都是通过消息来传递的,所以同一时间只有一个线程能够访问单线程套间中的对象。

三:
C#中,[STAThread]代表什么意思?如何用?
Single Thread Apartment

>Why is STAThread required?

it changes the apartment state of the current thread to be single threaded

>Single Thread Apartment vs MultiThread Apartment?

Correct: With the STAThread attribute, you will be interacting with COM processes in a
"Single Threading Apartment" model. Without it, you will be interacting with COM processes
in the "Multiple Threading Apartment" model.

> so why do I need it....or why would I want it at some point?

You may want to interact with a COM process in a MTA model for performance reasons. You may
want to interact with a COM process in a STA model because of a design requirement. For example,
to use the Windows clipboard (System.Windows.Forms.Clipboard) you must be calling from a thread
running in a STA. If the calling thread was started by your application you can set the ApartmentState
(System.Threading.ApartmentState) before starting, but if you want to use the clipboard from your a
pplication's main thread, you need to use the System.STAThread attribute on your Main method.

> why does Main( ) only function as an entry point when it is declared static?

The simple answer is that is just the way that Microsoft designed the language. One way you can look at
this though, is there should only be 1 "instance" of your Main method - the main method has nothing to do
with any specific instances of the class it is defined in, and should therefore be static. In my opinion
it might have been a good idea to give the Main method a property similar to a static contructor where it is
executed once, and only once. Anyway, because the Main method is static, you can execute your program without
having to create any arbitrary objects.

【转】C#中[STAThread]的作用相关推荐

  1. VMware虚拟机文件夹中各文件作用详解

    VMware虚拟机文件夹中各文件作用详解 虚拟机的文件管理由VMware Workstation来执行. 一个虚拟机一般以一系列文件的形式储存在宿主机中, 这些文件一般在由workstation为虚拟 ...

  2. Science:亚硝酸盐氧化细菌在黑暗海洋中的主要作用

    文章目录 亚硝酸盐氧化细菌在黑暗海洋中的主要作用 写在前面 摘要 热心肠日报 背景 结果 图1 非全长16S rRNA基因序列的系统发育树 图2 通过募集宏基因组片段的方法确定亚硝酸盐氧化细菌单扩增基 ...

  3. 丰度决定了细菌在复杂群落中的功能作用

    本文转载自"微生态笔记",已获授权 标题:Abundance determines the functional role of bacterial phylotypes in c ...

  4. tensorflow2.0中valid_data的作用是在训练的过程对对比训练数据与测试数据的准确率 损失率,便于判断模型的训练效果:是过拟合还是欠拟合(过拟合)

    tensorflow2.0中valid_data的作用是在训练的过程对对比训练数据与测试数据的准确率,便于判断模型的训练效果:是过拟合还是欠拟合 过拟合:训练数据的准确率较高而测试数据的准确率较低 欠 ...

  5. C#中构造函数的作用

    C#中构造函数的作用 共同点: 都是实例化对象,初始化数据的 默认构造是说所有的类都从祖先object那继承了空参的构造方法,你不写与写空参构造都存在,而有参数的构造一般是自己写的,写就有不写就没有, ...

  6. python里dir是什么意思_python中dir什么作用

    python中dir的作用是:1.dir函数传入数据类型返回该数据类型的所有内置方法:2.dir函数传入模块名返回该模块的所有属性和方法. dir() 内置函数的作用 python 内置方法有很多,无 ...

  7. 浅析SEO优化中标签的作用?

    目前,已经有很多企业网站有专门的优化人员来进行SEO优化,在网站SEO优化的过程中会使用到很多标签来利于搜索引擎的读取,提升网站的排名和权重.就以大连网龙科技旗下产品云霸屏为例来讲,云霸屏作为一款全网 ...

  8. Hibernate 中lazy的作用

    Hibernate 中lazy的作用 上一篇 / 下一篇  2009-06-24 20:02:20 / 个人分类:java 查看( 506 ) / 评论( 2 ) / 评分( 5 / 0 ) < ...

  9. 【Android 逆向】Android 进程注入工具开发 ( 调试进程中寄存器的作用 | 通过 EIP 寄存器控制程序运行 | EIP 寄存器的存档与恢复 )

    文章目录 一.调试进程中寄存器的作用 二.通过 EIP 寄存器控制程序运行 三.EIP 寄存器的存档与恢复 一.调试进程中寄存器的作用 内存是一个线性结构 , 将动态库加载到内存中后 , 每个动态库文 ...

最新文章

  1. 第十六届全国大学生智能车各分赛区所需要的比赛系统器材
  2. 一文弄懂用户画像以及如何召回用户
  3. @ResponseBody,@RequestBody,@PathVariable
  4. 代码实现tan graph model for classification_几行代码搞定ML模型,低代码机器学习Python库正式开源...
  5. Java基础篇:去探索String类
  6. 第三方app_官方打不过第三方APP之 知乎 篇
  7. The Book of Mozilla
  8. 如何在win10上显示隐藏文件
  9. 高分一号二号PMS数据处理
  10. iOS-APP性能测试
  11. Android连接蓝牙打印机
  12. 微信公众号支付的回调地址 不起作用的问题
  13. 移动IP技术概述(转)
  14. Navicat mysql 数据库备份和使用,备份以后是nb3文件
  15. java.lang.IllegalArgumentException异常
  16. Bootstrap学习-详解Bootstrap下拉菜单组件
  17. 如何分析个股基本面_如何从基本面判断个股?
  18. 高等数学教程【单变量微积分】内容目录
  19. c8051f340定时器2中断以及初值设置
  20. 【项目精选】基于Java的模拟写字板的设计与实现(视频+论文+源码)

热门文章

  1. 【OS学习笔记】三十二 保护模式九:分页机制对应的汇编代码之---内核代码
  2. mysql数据库思维导图
  3. [NLP] 相对位置编码(二) Relative Positional Encodings - Transformer-XL
  4. [SimplePlayer] 2. 在屏幕上显示视频图像
  5. C#学习笔记-数据的传递(公共变量)以及Dictionary
  6. bzoj 2905 背单词
  7. 解密多媒体封装解封装框架
  8. Mysql找不到mysql.sock怎么办?
  9. Ref and Out
  10. [Leetcode][第35题][JAVA][搜索插入位置][二分法]