zoukankan      html  css  js  c++  java
  • Difference between STA and MTA in thread

    Link:

    Could you explain STA and MTA?

    STA: Single-Thread Apartment, 中文叫单线程套间。就是在COM库初始化的时候创建一个内存结构,然后让它和调用CoInitialize的线程相关联。这个内存结构针对每个线程都会有一个。支持STA的COM对象只能在创建它的线程里被使用,其它线程如果再创建它就会失败


    MTA: Mutil-Thread Apartment,中文叫多线程套间。COM库在进程中创建一个内存结构,这个内存结构在整个进程中只能有一个,然后让它和调用CoInitializeEx的线程相关联。支持MTA的COM对象可以在任意线程里被使用。多有针对它的调用都会被封装成为消息。
    STA (单线程单元) MTA
    Windows NT 3.51引入 Windows NT 4.0 引入
    只允许一个实例线程访问组件,不需要考虑多个线程访问实例变量的问题,因为这种保护是由COM特性实现的 多个线程可以同时访问组件
    COM对象不是线程安全,需要在注册表中把注册键ThreadingModel设置为Apartment COM对象线程安全,ThreadingModel设置为Free。值Both用于不考虑单元类型的线程安全的COM对象


    其实STA和MTA是COM规定的一套线程模型,用于保障多线程情况下你的组件代码的同步。比如说有一个COM对象它内部有一个静态变量 gHello,那么这个对象无论生成多少实例对于gHello在内存中只能有一份,那么如果有两个不同的实例在两个线程里面同时去读写它,就有可能出错,所以就要就要有种机制进行同步保护,STA或者MTA就是这种机制。

    .NET支持两种线程模型:STA和MTA。
    STA(single threaded apartments)。apartment只是一个逻辑上的概念,它可以包含一个或多个线程。一个AppDomain可以包括一个或多个apartment。STA是指该apartment中只能包含一个thread。
    MTA(multi threaded apartments)。指该apartment中可以包含多个thread。

    STA and MTA 之间最大的区别就是MTA 可以在同一个apartment 中使用所有的共享资源并发执行多个线程。 而多个STA虽然可以共享数据,但是不能并发执行线程,存在性能问题。

    线程的创建:
    当创建一个新的STA线程时,CLR会在该AppDomain中创建一个apartment和thread(从属于该apartment)。如果是创建MTA线程,CLR会检查该AppDomain是否存在一个存放MTA的apartment,如果存在仅创建该线程到该MTA中,否则就创建一个MTA和thread(从属于该apartment)。
    我们可以设置线程的属性。例如 t.ApartmentState = ApartmentState.STA;

    线程的使用区别:
    我们应该仅仅在访问STA-based 的COM组件时才使用STA线程模式。可以在注册表的HKEY_CLASSES_ROOT/CLSID/{Class ID of the COM component} /InProcServer32 下查看到该COM的线程模式。如果该值是Apartment,则说明该COM只能以STA模式运行。其他的值有Free(MTA),Both(STA+MTA),Single(只能在一个单一的线程中执行)。
    其他情况下,我们应该使用MTA的线程,虽然需要我们费心线程间资源的同步问题。


    每个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.


    示例:
    我现在想在一个windows form的程序中实现从某个word文档复制图片并保存的方案。
    具体是:打开word文档,将图片信息复制到粘贴板中,然后从粘贴板中取得图片信息,再保存到本地目录中。

    如果在某个按钮的事件中,直接调用该方法,那么界面将变得没有响应。所以我们需要考虑使用多线程来解决这个问题。Thread t = new Thread(new TheardStart(CopyImages); t.Start();
    如果是这样,则程序会发生错误.。要么显示出现异常,要么没异常但是Clipboard为空,取不到任何数据!为什么呢?
    因为Word.Application 是Automation并且STA-Based,不能在没有指定ThreadApartment的线程中被调用。所以导致了各种错误,所以需要在t.Start();前面加上t.Apartment = ApartmentState.STA;这样就完全正常了。
    对于MTA的多线程我们就见的比较多了,不再举例了。

    另外一点不明白,我监视任务管理器发现,我在执行Thread t = new Thread(new TheardStart(CopyImages);t.Apartment = ApartmentState.STA; t.Start();之后该程序的进程中线程数从3个增加到6个,如果创建的是MTA的线程则只增加1。我的理解是STA线程为需要维护内部隐藏的窗口类和消息队列而增加的。

    下面是实现方法:


    <!--

    Code highlighting produced by Actipro CodeHighlighter (freeware)
    http://www.CodeHighlighter.com/

    --> 1private void CopyImages()
     2        {
     3            Word.Application app = null;
     4            Word.Document doc = null;
     5            
     6            app = new ApplicationClass();
     7            
     8            try
     9            {
    10                object fileName = @"E:/A.doc";
    11                doc = app.Documents.Open(ref fileName,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,ref missing,
    12                    ref missing,ref missing,ref missing,ref missing,ref missing,ref missing);
    13
    14                int count = doc.InlineShapes.Count;
    15                for(int i=1;i<=count;i++)
    16                {
    17                    doc.InlineShapes[i].Range.Copy();
    18                    
    19                    if (Clipboard.GetDataObject() != null
    20                    
    21                        IDataObject data = Clipboard.GetDataObject(); 
    22
    23                        if (data.GetDataPresent(DataFormats.Bitmap)) 
    24                        
    25                            Image image = (Image)data.GetData(DataFormats.Bitmap,true); 
    26                            image.Save("E://" + i.ToString() + ".jpg",System.Drawing.Imaging.ImageFormat.Jpeg); 
    27                        }
     
    28                        else 
    29                        
    30                            lst_Items.Items.Add(doc.Name + ";无正确图片数据");
    31                        }
     
    32                    }
     
    33                    else 
    34                    
    35                        lst_Items.Items.Add(doc.Name + ";粘贴板为空");
    36                    }
     
    37                }

    38                
    39            }

    40            catch(Exception ex)
    41            {
    42                lst_Items.Items.Add(doc.Name + "发生错误;" + ex.Message);
    43            }

    44            finally
    45            {
    46                if (doc != null)
    47                    doc.Close(ref missing,ref missing,ref missing);
    48                if (app != null)
    49                    app.Quit(ref missing,ref missing,ref missing);
    50            }
    作者:Angelo Lee
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
  • 相关阅读:
    心态--编程+成长
    【Oracle】容易犯的错误和技巧集合
    【WPF】DataGrid的Row样式设置
    【WPF】给TextBox添上Label
    【Oracle】异常信息的加工处理
    【.Net 】Json和Xml解析
    【Win32 API】利用SendMessage实现winform与wpf之间的消息传递
    【WCF】利用WCF实现上传下载文件服务
    【Oracle】实现Oracle数据库对象的一键升级
    感触
  • 原文地址:https://www.cnblogs.com/yefengmeander/p/2887764.html
Copyright © 2011-2022 走看看