帮忙看一下[STAThread]这个是什么

朝三慕四 2010-11-10 01:49:14
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());
}
...全文
341 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
fdffdfdfdffdfdfdf 2011-02-20
  • 打赏
  • 举报
回复
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.Forms

NameSpace UsageLibrary

Public 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 Sub

End Class

End 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);
}
}
}



darksmile 2010-11-11
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 lianaiqiong 的回复:]

引用 6 楼 darksmile 的回复:
换个位置

public partial class MainForm
{
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}

[STAThread]
publ……
[/Quote]
STAThread 是个特性
并且是一个只能用来修饰方法的特性
所以必须放在方法的前面,比如放在Main方法的前面。

放在字段、属性、类前面都是不对的。
朝三慕四 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 darksmile 的回复:]
换个位置

public partial class MainForm
{
private string m_filename;
public string FileName
{
get { return m_filename; }
set { m_filename = value; }
}

[STAThread]
public static void ……
[/Quote]
解释 下下哈....谢谢....
ZengHD 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 xrongzhen 的回复:]

用来修饰入口方法
[/Quote]
那为什么要修饰呢?不修饰行不行?
xrongzhen 2010-11-10
  • 打赏
  • 举报
回复
用来修饰入口方法
ZengHD 2010-11-10
  • 打赏
  • 举报
回复
难道是等于CoInitialize
darksmile 2010-11-10
  • 打赏
  • 举报
回复
换个位置

public partial class MainForm
{
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());
}
朝三慕四 2010-11-10
  • 打赏
  • 举报
回复
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());
}

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中声明有效”怎么改....
全栈深入 2010-11-10
  • 打赏
  • 举报
回复
STAThread 指示一个应用程序的COM线程模型是单线程单元.

使用 Windows 窗体的任何应用程序的入口点上必须存在此特性;
1.如果没有此特性,则 Windows 组件可能无法正常工作。
2.如果不存在此特性,则应用程序将使用 Windows 窗体不支持的多线程单元模型

应用程序框架的 Visual Basic 项目不必使用 STAThread 标记 Main 方法。因为Visual Basic 编译器会自动进行标记。
yjxgbcl 2010-11-10
  • 打赏
  • 举报
回复
用com组件
xiaowei5780651 2010-11-10
  • 打赏
  • 举报
回复
google呀
darksmile 2010-11-10
  • 打赏
  • 举报
回复
STAThread 表示单线程模式

只有当你的代码中使用了COM组件时有效
如果没有用到任何COM组件,那么没有任何作用。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧