是否重大更改 否 原因 某程序集引用了 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); } } }
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]区别在哪里呢?
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并不是指不能使用多个线程,它也可以使用多线程的.
可以不加.2. 一个误解.STAThread并不是表示你的程序只能编写单线程序,完全可以有多个线程.
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);
}
}
}