public partial class MainForm
{
[STAThread]
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
{
[STAThread]
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
如果没有用到任何COM组件,那么没有任何作用。
1.如果没有此特性,则 Windows 组件可能无法正常工作。
2.如果不存在此特性,则应用程序将使用 Windows 窗体不支持的多线程单元模型应用程序框架的 Visual Basic 项目不必使用 STAThread 标记 Main 方法。因为Visual Basic 编译器会自动进行标记。
{
[STAThread]
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void Button1Click(object sender, System.EventArgs e)
{
openFileDialog1.Filter="Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"; if(openFileDialog1.ShowDialog()==DialogResult.OK)
FileName=openFileDialog1.FileName;
............................................
}
}
}
错误是“属性[STAThread]在该声明中无效,它只在method中声明有效”怎么改....
{
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
并且是一个只能用来修饰方法的特性
所以必须放在方法的前面,比如放在Main方法的前面。放在字段、属性、类前面都是不对的。
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);
}
}
}