首页 > 编程语言 >【C# 】STAThread

【C# 】STAThread

时间:2023-08-25 18:45:02浏览次数:32  
标签:多线程 thread C# 线程 NET STAThread COM

[STAThread]
STAThread:Single     Thread     Apartment Thread.(单一线程单元线程)
[]是用来表示Attributes;

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

[STAThread] attribute指示应用程序的 COM 线程模型是单线程单元。
而于此对应的多线程单元则是 [MTAThread] (多线程单元线程)

COM 线程模型只适用于使用 COM interop(托管代码与非托管代码之间的协同过程) 的应用程序。如果将此属性应用到不使用 COM interop 的应用程序,将没有任何效果。

COM 线程模型可设置为单线程单元或多线程单元。如果应用程序线程实际调用了 COM 组件,则仅为 COM interop 初始化该线程。如果没有使用 COM interop,则不初始化该线程。

以下是找到的一个资料介绍:
Q. When I create a c# project from scratch in VS.NET, the generated code always have a [STAThread] attribute above the main routine. What does the STAThread attribute really do? Can I change it to MTAThread instead? I have searched website and books, no one seems to explain this well.

Asked by anon. Answered by the Wonk on February 17, 2003

 

A.

The STAThreadAttribute marks a thread to use the Single-Threaded COM Apartment if COM is needed. By default, .NET won't initialize COM at all. It's only when COM is needed, like when a COM object or COM Control is created or when drag 'n' drop is needed, that COM is initialized. When that happens, .NET calls the underlying CoInitializeEx function, which takes a flag indicating whether to join the thread to a multi-threaded or single-threaded apartment.

A multi-threaded apartment (MTA) in COM is more efficient, since any of a number of RPC threads from a pool can be used to handle a request. However, an object on the MTA thread needs to protect itself from multiple threads accessing it at the same time, so that efficiency comes at a cost.

The single-thread apartment (STA) in COM is inherently single-threaded and therefore no additional thread synchronization is needed. The STA is implemented using the thread's Windows message queue, which is how requests to objects on an STA are serialized. Because of how the STA thread is implemented, calls to objects on that thread are serialized with Windows message handling on that thread, making sure that everything, both the COM objects and the underlying windowing objects, e.g. HWNDs, are all synchronized. This is necessary for UI-oriented COM objects, like controls and drag 'n' drop, which must also be synchronized together with the UI.

When COM is needed .NET will call CoInitializeEx, picking the MTA by default because it's more efficient. However, to get the synchronization needed for controls, windows and drag 'n' drop, you need to mark a thread's entry point with the STAThreadAttribute to let .NET know to initialize the UI thread on the STA. All of the VS.NET project templates put that attribute in to make sure you don't forget:
大致意思是:由于很多COM在.NET环境下如果使用多线程的话,会导致引用的COM不能正常运行,而如果不声明程序为STAThread的话,.NET就会自动使用多线程来提高效率,这样就会导致不可预知的后果。

标签:多线程,thread,C#,线程,NET,STAThread,COM
From: https://www.cnblogs.com/luyj00436/p/17207405.html

相关文章

  • Chat Emy会让程序员失业吗?
    ChatEmy具备强大的自动编码能力,可以理解、解析和生成大量的自然语言。传统上,人们需要花费大量的时间和精力来编写和调试代码,但ChatEmy可以通过学习海量数据,自动完成这些复杂的任务,从而减少了程序员的工作量。这让一些人担心,程序员在代码编写方面的需求可能会减少。一些人担心这一......
  • 记录--一个炫酷的css动画
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助最近有一个需求,要我实现一个动画效果,效果如下简单分析了一下效果,是一个3d的效果,首先是一个圆,接着是两段圆环,第三层是一堆小圆环,最里面是一些线上运动,有着渐变色的矩形。第一层的圆环很简单。第二层的圆环其实......
  • java-结束端口对应的process
    importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassProcessKiller{publicstaticvoidmain(String[]args){intport=8080;//要释放的端口号try{//构造命令......
  • CentOS 7 上更新系统时间和时区
    要在CentOS7上更新系统时间和时区(CST,即中国标准时间),可以按照以下步骤进行操作:更新系统时间:首先,你可以使用以下命令来手动更新系统时间为当前时间:sudodate+%T-s"hh:mm:ss"替换"hh:mm:ss"为当前的小时、分钟和秒。你也可以更新日期和时间一起,例如:sudodate+%Y%m%......
  • centos7实现开机自启动Python脚本
    在Linux系统中,你可以使用不同的方法来实现开机自启动Python脚本和确保守护进程不挂掉。以下是一些常用的方法:1.开机自启动Python脚本:使用rc.local文件(适用于一些旧的系统):编辑/etc/rc.local文件,将你希望在系统启动时运行的命令或脚本添加到文件中。确保给脚本提供正确的路径。......
  • Jenkins +miniprogram-ci 构建 发布、预览微信小程序
           #!/bin/bash-lrm-rfqrcode*.jpgyarnyarnwxcitype=$actionappid=$appidversion=$versiondesc=$descbuildId=${BUILD_ID}#计算过期时间,并将过期时间写进自定义环境变量#计算过期时间,并将过期时间写进自定义环境变量now=`date'+%Y-%m-%d%H......
  • [AGC030D] Inversion Sum
    题目大意一个长度为\(n\)的数列,然后给你\(q\)个交换或不交换操作,你可以选择操作或者不操作,问所有情况下逆序对的总和。答案需要对\(10^9+7\)取模。(\(n\leq3000\),\(q\leq3000\))。思路这道题非常巧妙。我们先考虑转化题意,求逆序对数量的期望。记\(dp_{i,j}\)表......
  • OceanBase通过基表检索数据库中的函数索引
    其实通过dba_indexes这个视图也能检索出来,但是如果通过index_type来过滤性能会极差,实际效率会差很多,可能十几秒中才会出来结果,下面是通过基表视图跳过index_type来检索函数索引。 ......
  • 视频集中存储/云存储平台EasyCVR国标GB28181协议接入的报文交互数据包分析
    安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快,可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等,以及支持厂家私有协议与SDK接入,包括海康Ehome、海大宇等设备的SDK等。视频汇聚融合管理平台EasyCVR既具备传统安防视频监控的能力,......
  • Oracle Linux 8 yum 安装 MySQL 8
    一、安装yum仓库rpm-ivhhttps://dev.mysql.com/get/mysql80-community-release-el8-8.noarch.rpm二、先禁用本地的MySQL模块yummodule-ydisablemysqlMySQL8.0CommunityServer......