我发现在windows窗体应用程序的program.cs中有使用[STAThread]指定单线程单元,
而控制台应用程序则没有,
那么有了这个[STAThread]是否表示只能单线程运行,改成[MTAThread]才能使用多线程呢?

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using System.Threading;namespace 电子相册DIY
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [MTAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new control());
                Thread t1 = new Thread(nMain);
                t1.Start();
            }
            static void nMain()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new control());
            }
        }
    }
    以上代码有两个线程,一个主线程和一个thread t1
    这个program.cs运行后还是只能每次启动一个窗体应用程序,不能同时打开两个应用程序,那 [MTAThread]
    和 [STAThread]区别在哪里呢?
      

  2.   

    当你想指定工程的启动窗口的时候,你需要在该窗口类中申明一个Main()方法,并为这个方法设置[STAThread]属性。
      

  3.   

    http://blog.joycode.com/vbcti/archive/2008/08/29/115248.joy//STAThreadAttribute 
    http://msdn.microsoft.com/zh-cn/library/system.stathreadattribute.aspx//MTAThreadAttribute 
    http://msdn.microsoft.com/zh-cn/library/system.mtathreadattribute.aspx?PHPSESSID=tn8k5...http://www.cnblogs.com/huashanlin/archive/2007/07/07/809305.html
    STAThreadAttribute和MTAThreadAttribute 只是标示当前线程的一个属性,STAThreadAttribute并不是指不能使用多个线程,它也可以使用多线程的.
      

  4.   

    我对STAThread的理解:1. STAThread只在你的程序里有com互操作时才起作用,所以如果你的程序里边全部为托管代码时,该属性
       可以不加.2. 一个误解.STAThread并不是表示你的程序只能编写单线程序,完全可以有多个线程.
      

  5.   

    这和是否是多线程无关,单纯针对控件而言,使用多线程的人一般知道,当我们在跨线程操作控件时,有时会有意想不到的异常发生,多数时会提示“不能修改不是由该线程创建的控件”,这就是跨线程操作控件的问题。这时可以通过委托回调操作该控件,但是对于指定在[STAThread]下运行的控件,如果你跨线程的话,两个线程必须都是[STAThread]的,但是一旦指定了[STAThread],这两个线程就互相独立,不能互相干涉了,比如回调操作另一个线程将不可能。
      

  6.   

    TypeName
     MarkWindowsFormsEntryPointsWithStaThread
     
    CheckId
     CA2232
     
    类别
     Microsoft.Usage
     
    是否重大更改
     否
     原因
    某程序集引用了 System.Windows.Forms 命名空间,但没有使用 System..::.STAThreadAttribute 属性标记该程序集的入口点。规则说明
    STAThreadAttribute 指示应用程序的 COM 线程模型是单线程单元。使用 Windows 窗体的任何应用程序的入口点上必须存在此属性;如果没有此属性,则 Windows 组件可能无法正常工作。如果不存在此属性,则应用程序使用 Windows 窗体不支持的多线程单元模型。注意: 
    使用应用程序框架的 Visual Basic 项目不必使用 STAThread 标记 Main 方法。Visual Basic 编译器会自动进行标记。
     如何修复冲突
    要修复与该规则的冲突,请将 STAThreadAttribute 属性添加到入口点。如果存在 System..::.MTAThreadAttribute 属性,请移除该属性。何时禁止显示警告
    如果为不需要并且不支持 STAThreadAttribute 属性的 .NET Compact Framework 进行开发,则可以安全地禁止显示与该规则有关的警告。示例
    下面的示例演示 STAThreadAttribute 的正确用法。Visual Basic  复制代码 
    Imports System
    Imports System.Windows.FormsNameSpace UsageLibraryPublic Class MyForm
       Inherits Form   Public Sub New()
          Me.Text = "Hello World!"
       End Sub 'New   ' Satisfies rule: MarkWindowsFormsEntryPointsWithStaThread.
       <STAThread()> _
       Public Shared Sub Main()
          Dim aform As New MyForm()
          Application.Run(aform)
       End SubEnd ClassEnd Namespace
     
    C#  复制代码 
    using System; 
    using  System.Windows.Forms;namespace UsageLibrary
    {
        public class MyForm: Form
        {
            public MyForm()
            {
                this.Text = "Hello World!";
            }        // Satisfies rule: MarkWindowsFormsEntryPointsWithStaThread.
            [STAThread]
            public static void Main()
            {
                MyForm aform = new MyForm();
                Application.Run(aform);
            }
        }
    }