STAThread 初学习集合
来源:互联网 发布:ubuntu 14.04 jdk1.8 编辑:程序博客网 时间:2024/05/16 03:52
http://blog.sina.com.cn/s/blog_4493d3b50100u6vi.html
[STAThread]--Single-Thread Apartment
在C#中用在Main()函数之前,指定当前线程的ApartmentState是STA,用在其他方法上没有作用。
有时根据设计需要,需要同COM进程进行交互,例如使用Windows clipboard。
以下引用另一同辈的发言: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
也就是说如果你用的.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;
入门就这些问题。
打开Visual Studio .NET 2003 命令提示键入ILDASM,这个程序可以查看编译后的元数据。
网上查一下Reflector这个软件。干什么用的。反编译呀。。。。寒。
=========================================================================
单线程套间(STAThread) vs. 多线程套间(MTAThread )
[原文链接]:
======================================================================================
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"; } }}
- STAThread 初学习集合
- [C#]STAThread 初学习
- [C#]-STAThread 初学习
- STAThread
- STAThread
- [STAThread]
- 集合初学
- 关于STAThread
- 解读STAThread
- [STAThread]是什么
- C# [STAThread]
- C# [STAThread]
- 关于[STAThread]
- C#[ STAThread]
- 初学SWift_集合 字符串
- JAVA初学--集合类
- Python初学教程集合
- java之集合框架初学
- PHP PDO ACCESS 留言板-编辑留言editMessage.php
- 试玩小兵赚钱真的假的
- 关联同一张表两次
- 转VS2010下MFC的串口编程
- rust 交叉编译平台: 在ubuntu上编译windows程序
- STAThread 初学习集合
- 从C++到Qt
- iOS字符串操作
- map中 key,value 对换 ,互换
- leetcode 240. Search a 2D Matrix II
- Swiper使用
- android 中使用jwt token(json web token)--java
- 最大后验估计(MAP)与最大似然估计(MLE)
- bzoj 1116(构造+并查集)