Eclipse开发平台 Eclipse集成开发环境 Java技术参考 J2EE企业应用项目 J2EE开发框架整合应用
Visual Studio 2008 技术 Visual Studio 2008集成环境 LINQ SQL Server 2008数据库 Silverlight WCF WPF WWF
平台综合 开发平台技术动态 跨平台开发 软件信息技术与商业 国外媒体技术资讯
程讯网下载 精品代码 — J2EE应用下载 精品代码 — ASP.NET应用 常用J2EE开发框架 流行ASP.NET开发框架

搜索
Google
 
最新文章
对“职业生涯及规划”的一些想法与建议
 
Visual Studio 10将会怎样?
 
三十怎么了? 心态最重要!
 
访问Microsoft格式文件的Jakarta-POI API库的.NET版本
 
dashCommerce 3.X如何安装Microsoft SQL Server 2005 Express Edition的高级服务功能
 
.net 2.0运行时提示以下错误:authentication mode=Windows 解决方法如下
 
在浏览器中发贴子时实现自动添加贴子签名档的BHO对象设计
 
C#下用Browser Helper Object对象实现拦截IE浏览器的各项消息的IE插件
 
Build a Managed BHO and Plug into the Browser
 
在C#中用WM_COPYDATA消息来实现进程间通信的详细编码
 
BHO 浏览器辅助对象关联原理、编写流程
 
Oracle8i的卸载
 
Oracle9i图形工具OEM的简介
 
详细介绍整个Oracle9i软件的安装过程
 
C# 面向对象设计模式纵横谈 - 18、Iterator迭代器(行为型模式)
 
 
C#程序的主函数写[STA Thread] 属性是什么目的?(What is the purpose of the [STA Thread] attribute for the Main method of a C# program? ) - Visual Studio常见问题FAQ、Knowledge base知识库、使用经验和技巧
 
内容摘要 -


全文 -

C#程序的主函数写[STA Thread] 属性是什么目的?(What is the purpose of the [STA Thread] attribute for the Main method of a C# program? )

C#中,[STAThread]代表什么意思?如何用?

> 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 application'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.


单线程套间,简单来说所有对于单线程套间中对象的访问都是通过消息来传递的,所以同一时间只有一个线程能够访问单线程套间中的对象。


 
相关内容
查找更多 ◇ Visual Studio常见问题FAQ、Knowledge base知识库、使用经验和技巧