C#中,[STAThread]代表什么意思?

yinliangzhi 2005-07-08 05:33:50
C#中,[STAThread]代表什么意思?如何用?
...全文
5153 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
logan2009 2010-06-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 wjlrual 的回复:]
up
[/Quote]不是,你可以看看CheckForIllegalCrossThreadCalls属性相关的帮助!可能有点帮助!
michaelwang_1978 2005-08-20
  • 打赏
  • 举报
回复
“/Webregister”应用程序中的服务器错误。
--------------------------------------------------------------------------------

指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

源错误:


行 105:
行 106:
行 107: sqlcomm.Parameters["@name_stud"].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text;
行 108:
行 109:


源文件: c:\inetpub\wwwroot\webregister\webform2.aspx.cs 行: 107

堆栈跟踪:


[InvalidCastException: 指定的转换无效。]
Webregister.WebForm2.DataGrid1_UpdateCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\webregister\webform2.aspx.cs:107
System.Web.UI.WebControls.DataGrid.OnUpdateCommand(DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


我在做一个DataGrid 更新 产生这样一个异常 到底是为什么? 请高人帮忙~

HopeInDark 2005-07-10
  • 打赏
  • 举报
回复
mark
yinliangzhi 2005-07-10
  • 打赏
  • 举报
回复
在C#中,默认的就是,[STAThread].我要做socket通信程序,需要服务器端与多个客户端通信.在服务器端再用这个[STAThread]是不是就不行了吧?
wjlrual 2005-07-08
  • 打赏
  • 举报
回复
up
tmfc 2005-07-08
  • 打赏
  • 举报
回复
单线程套间,简单来说所有对于单线程套间中对象的访问都是通过消息来传递的,所以同一时间只有一个线程能够访问单线程套间中的对象。
AdmLfa 2005-07-08
  • 打赏
  • 举报
回复
谁能用中文简要说下
ihsgnep 2005-07-08
  • 打赏
  • 举报
回复
关于 COM的书有 介绍
我对它的认识也很模糊
michaelwang_1978 2005-07-08
  • 打赏
  • 举报
回复
到底什么意思?
孟子E章 2005-07-08
  • 打赏
  • 举报
回复
> 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 application'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.
孟子E章 2005-07-08
  • 打赏
  • 举报
回复
Why is STAThread required?


it changes the apartment state of the current thread to be single threaded

http://blogs.msdn.com/jfoscoding/archive/2005/04/07/406341.aspx
tiaoci 2005-07-08
  • 打赏
  • 举报
回复
Single Thread Apartment

111,097

社区成员

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

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

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