C#中[STAThread]的作用和c#入门的问题

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.

===============================================================================

[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就会自动使用多线程来提高效率,这样就会导致不可预知的后果。

 


以下引用另一同辈的发言:http://blog.csdn.net/qilang/archive/2006/06/06/775605.aspx
STA不是单线程的意思.英文为single threaded apartment.是一种套间(或译为公寓)线程模式.

sta thread并不表明应用程式的类型,和应用程序不搭界,恰相反,一个应用程序可以有多个线程.每个线程也可以有多个组件或对象.以前win16位系统的组件线程模式才真正是单线程.这是一种被淘汰了的模式.
线程模式用于处理组件在多线程的环境里并行与并互的方式.比如套间线程(STAThread)模式中接口跨线程传递必须被调度(Marshal),不调度直传肯定会失败!而MTA或FreeThread模式中的接口可以不经调度直接传递.
这种调度在特定的环境中非常影响性能(可有几百倍之差).如VB里只支持STAThread模式.FreeThread模式的组件会在里面表现成和跨进程一样慢!
线程模式是微软的COM基础中的极其重要的概念.一定要吃透!
我对.net真是一窍不通(没空去弄,对不起微软去年的奖赏).但我可以肯定,C#中的[STAThread]属性是应用程序的套间初始化代码.可以直接理解成SDK里的
CoInitialize(NULL);
初始一个STA套间实际上是相当于开了一个消息窗口,所有调用经此窗口过程调度到组件内.
同理[MTAThread](不知有没有这个属性,自已去查)
可以理解成
CoInitializeEx(NULL,COINIT_MULTITHREADED )
这经常是一个对初入com大门的人来说,有一定难度但必须过的一道关.
=====================================================================================

C#学习笔记(一)-- 入门的困惑
简单的我就不写了,主要写一下C#学习中的要点和难点。
1.由HelloWorld开始
先看一段基本上每本C#书里都会讲到的例子,很老土。
using System;
namespace test
{
     class Class1
     {
         [STAThread]
         static void Main(string[] args)
         {
              System.Console.WriteLine("Hello,World!");
         }
     }
}
先引用一个命名空间System,再定义一个自己的命名空间test,里面有一个类Class1,属性[STAThread],一个入口的Main方法,注意:跟JAVA不一样,Main首名母是大写,Main必须是static的。不然怎么开始呢?难倒要实例化才行?哈哈,定义为static就是把它放在椎里。这里规举。
 
2.命名空间
再来看看System.Console.WriteLine("Hello,World!");输一名语名到控制台。调用System命名空间里的Console类的WriteLine方法。System命名空间是前面我们已经引用了的using System;
 
你也可以在引用的时候改个名字output,那么在调用的时候就是output.Console.WriteLine("Hello,World!");
试一试:
using output=System;
 
namespace test
{
     class Class1
     {
         [STAThread]
         static void Main(string[] args)
         {
              output.Console.WriteLine("Hello,World!");
         }
     }
}
运行报错:F:\mydoc\Visual Studio Projects\test\Class1.cs(7): 找不到类型或命名空间名称“STAThread”(是否缺少 using 指令或程序集引用?)
嘿嘿,是[STAThread]惹的祸。干掉它。再试,搞定。
[STAThread]是Single  Thread  Apartment单线程套间的意思。是一种线程模型。其它的好像还是MTA(多线程套间)、Free  Thread(自由线程)。这个属性要加在主  Main  上。这个属性只在  Com  Interop  所用,如果全部是  managed  code  则无用。简单的说法:[STAThread]指示应用程序的默认线程模型是单线程单元 (STA)。启动线程模型可设置为单线程单元或多线程单元。如果未对其进行设置,则该线程不被初始化。
也就是说如果你用的.NET Framework,并且没有使用COM Interop,一般不需要这个Attribute。
 
明白了吧。
 
注意,using指令是用于命名空间的。变化着用一下,也可以为类创建别名:
using output=System.Console;
 
namespace test
{
     class Class1
     {
         //[STAThread]
         static void Main(string[] args)
         {
              output.WriteLine("Hello,World!");
         }
     }
}
这样也行。。。
 
命令空间是可以嵌套的。如:
using System;
 
namespace test
{
     namespace t1
     {
         class Class1
         {
              static void Main(string[] args)
              {
                   System.Console.WriteLine("t1.Class1");
              }
         }
     }
 
     namespace t2
     {
         class Class2
         {
              static void Main(string[] args)
              {
                   System.Console.WriteLine("t2.Class2");
              }
         }
     }
}
运行,报错。我是故意的(台下:大骗子)。J不要扔砖头啊。不要这么容易就放弃嘛,要执着。
看错误F:\mydoc\Visual Studio Projects\test\Class1.cs(9): 程序“F:\mydoc\Visual Studio Projects\test\obj\Debug\test.exe”定义了不止一个入口点:“test.t1.Class1.Main(string[])”
 
因为你的命名空间test里定义了二个Main方法,所以呢,不用我说了吧。
using System;
 
namespace test
{
     namespace t1
     {
         class Class1
         {
              static void Main(string[] args)
              {
                   System.Console.WriteLine("t1.Class1");
                   System.Console.WriteLine(t2.Class2.MyFunction());
              }
         }
     }
 
     namespace t2
     {
         class Class2
         {
              public static string MyFunction()
              {
                   return "t2.Class2";
              }
         }
     }
}
 
外部程序引用的时候就是这样:using test.t1;或using test.t2;
 
h
打开Visual Studio .NET 2003 命令提示键入ILDASM,这个程序可以查看编译后的元数据。
 
网上查一下Reflector这个软件。干什么用的。反编译呀。。。。寒。
=========================================================================
单线程套间(STAThread) vs. 多线程套间(MTAThread )
[原文链接]:  STA Thread vs. MTAThread(WHost)

 

          最近我遇到一个有趣的线程问题,想跟大家分享一下,以免大家也遇到这种容易混淆的问题。

          打开一个其他人写的C#程序,为了下面的讲解,我把这个程序叫作”DeltaEngine”。DeltaEngine会调用一个本地的程序集,并处理其中某些事件。然后建一个工程,把DeltaEngine作为库项目引用,这个工程又被一个VB程序引用。我把这个VB程序叫做"VBApp"。所以,引用结构就像: 

         VBApp (VB) -> DeltaEngine (C#) -> NativeCode

      当我把VBApp作为启动程序运行时,它会一直等待NativeCode里面的事件在DeltaEngine中被处理。我注意到,如果我把DeltaEngine作为启动程序来运行的话,事件就会像预期那样得到处理。我花了很多时间想解决这个问题,但还是很困惑。

      最后有人指出,VB项目的默认线程模型是单线程套间(SingleThreaded  Apartment),而C#项目的默认线程模型是多线程套间 (Multithreaded Apartment)。因为DeltaEngine最初是作为C#工程开发的,被默认为使用MTA。当使用VBApp作为启动程序调用DeltaEngine时,DeltaEngine就会使用STA。因为这样,DeltaEngine就会开始等待其实它已经唤起的事件,所以DeltaEngine会一直等待下去。如果我把DeltaEngine作为启动程序运行,它就会使用MTA并且会像预期那样工作。

      解决的方法就是把MTAThreadAttribute加进VBApp的主函数里面。如下:

    <MTAThread> Sub Main()

      类似,如果你想把STAThread attribute加进C#的主方法里面,如下:

      [STAThread]

      static void Main()

      WindowsForms程序要求使用STA,所以创建C# Winapp后,你可以在Program.cs看见像这样的代码。

      我就不重复一些已经有的关于STATread 和MTAThread的文件,所以我给出下面的一些link可以学到更多关于STATread 和MTAThread的东西。

     STAThreadAttribute

      http://msdn2.microsoft.com/en-us/library/system.stathreadattribute(VS.71).aspx                          

      http://blogs.msdn.com/jfoscoding/archive/2005/04/07/406341.aspx

      MTAThreadAttribute

      http://msdn2.microsoft.com/en-us/library/system.mtathreadattribute(VS.71).aspx

======================================================================================
C# 中WindowsForm 的初始化信息 [STAThread]
static class Program

    {

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());

        }

 

 

    }

 

以上的代码是VS 2005中默认的初始化信息.

Application.EnableVisualStyles();

简单的说就是让你的控件显示出来.当然是在WindowsForm 中

此方法为应用程序启用可视样式。如果控件和操作系统支持视觉样式,则控件将以视觉样式进行绘制。若要使 EnableVisualStyles 生效,必须在应用程序中创建任何控件之前调用它;EnableVisualStyles 通常是 Main 函数的第一行.

 

下面的代码示例演示如何在 Main 函数中调用 EnableVisualStyles 来启用应用程序的视觉样式

  对于支持   FlatStyle   属性的控件,请确保将   FlatStyle   属性设置为   FlatStyle.System   值。 

 


using System;

using System.Drawing;

using System.Windows.Forms;

 

namespace VStyles

{

    public class Form1 : System.Windows.Forms.Form

    {

 

        private System.Windows.Forms.Button button1;

 

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.Run(new Form1());

        }

 

        public Form1()

        {

            this.button1 = new System.Windows.Forms.Button();

            this.button1.Location = new System.Drawing.Point(24, 16);

            this.button1.Size = new System.Drawing.Size(120, 100);

            this.button1.FlatStyle = FlatStyle.System;

            this.button1.Text = "I am themed.";

 

            // Sets up how the form should be displayed and adds the controls to the form.

            this.ClientSize = new System.Drawing.Size(300, 286);

            this.Controls.Add(this.button1);

 

            this.Text = "Application.EnableVisualStyles Example";

 

        }

    }

}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Controls 是 C# 的一个类,它是 System.Windows.Forms 命名空间的一部分。Controls 类提供了创建和管理 Windows 窗体应用程序的控件的功能。控件是用户界面的可视化元素,如按钮、文本框、标签等。 通过使用 Controls 类,您可以在窗体上添加、删除和操作各种控件。您可以设置控件的属性,如位置、大小、颜色等。您还可以处理控件的事件,如单击按钮、输入文本等。 以下是使用 Controls 类创建按钮控件的示例代码: ``` using System; using System.Windows.Forms; public class MyForm : Form { public MyForm() { Button myButton = new Button(); myButton.Text = "Click me!"; myButton.Location = new Point(50, 50); myButton.Click += new EventHandler(MyButton_Click); Controls.Add(myButton); } private void MyButton_Click(object sender, EventArgs e) { MessageBox.Show("Button clicked!"); } } public class Program { [STAThread] public static void Main() { Application.Run(new MyForm()); } } ``` 在上面的代码,我们创建了一个继承自 Form 的自定义窗体类 MyForm。在 MyForm 构造函数,我们创建了一个按钮控件,并将其添加到窗体的 Controls 集合。我们还订阅了按钮的 Click 事件,并在事件处理程序显示一个消息框。 这只是 Controls 类的一小部分功能,它提供了许多其他方法和属性来管理和操作控件。您可以根据您的需求使用 Controls 类的各种功能来开发 Windows 窗体应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值