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

yinliangzhi 2005-07-08 05:33:50
C#中,[STAThread]代表什么意思?如何用?
...全文
5119 12 打赏 收藏 转发到动态 举报
写回复
用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
SimpAutoUpdater c#自动升级 模块源码 可以集成到自己程序: 首先在VS为当前的主程序项目添加引用,引用“客户端”的“SimpleUpdater.exe”。 在VS,点开“解决方案管理器”相应项目的“属性”节点,打开 AssemblyInfo.cs 文件,在最下面添加上一行自动更新声明: //--添加这行标记表示支持自动更新, 后面的网址为自动更新的根目录. [assembly: FSLib.App.SimpleUpdater.Updateable("http://ls.com/update.xml")] 这步是必须的,否则请求检查更新时会抛出异常;代码的网址即上面提到的能访问到xml文件的网址。 如果您希望更加简单的使用而不用去加这样的属性,或者您想程序运行的时候自定义,您可以通过下列方式的任何一种方式取代上面的属性声明: 使用 FSLib.App.SimpleUpdater.Updater.CheckUpdateSimple("升级网址") 的重载方法。这个重载方法允许你传入一个升级包的地址; 在检查前手动设置 FSLib.App.SimpleUpdater.Updater.UpdateUrl 属性。这是一个静态属性,也就是说,您并不需要创建 FSLib.App.SimpleUpdater.Updater.UpdateUrl 的对象实例就可以修改它。 无论使用哪种方式,请确保在检查更新前,地址已经设置。 到这里,准备工作即告完成,为代码添加上检查更新的操作即可。 static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var updater = FSLib.App.SimpleUpdater.Updater.Instance; //当检查发生错误时,这个事件会触发 updater.Error += new EventHandler(updater_Error); //没有找到更新的事件 updater.NoUpdatesFound += new EventHandler(updater_NoUpdatesFound); //找到更新的事件.但在此实例,找到更新会自动进行处理,所以这里并不需要操作 //updater.UpdatesFound += new EventHandler(updater_UpdatesFound); //开始检查更新-这是最简单的模式.请现在 assemblyInfo.cs 配置更新地址,参见对应的文件. FSLib.App.SimpleUpdater.Updater.CheckUpdateSimple(); /* * 如果您希望更加简单的使用而不用去加这样的属性,或者您想程序运行的时候自定义,您可以通过下列方式的任何一种方式取代上面的属性声明: * 使用Updater.CheckUpdateSimple 的重载方法。这个重载方法允许你传入一个升级包的地址; * 在检查前手动设置 FSLib.App.SimpleUpdater.Updater.UpdateUrl 属性。这是一个静态属性,也就是说,您并不需要创建 FSLib.App.SimpleUpdater.Updater.UpdateUrl 的对象实例就可以修改它。 */ FSLib.App.SimpleUpdater.Updater.CheckUpdateSimple("升级网址"); Application.Run(new Form1()); } static void updater_UpdatesFound(object sender, EventArgs e) { } static void updater_NoUpdatesFound(object sender, EventArgs e) { System.Windows.Forms.MessageBox.Show("没有找到更新"); } static void updater_Error(object sender, EventArgs e) { var updater = sender as FSLib.App.SimpleUpdater.Updater; System.Windows.Forms.MessageBox.Show(updater.Exception.ToString()); } }
用官方汉化包汉化DXperience控件详细步骤!! 1.你要先安装某一版本的DXperience控件(如:DXperience-11.2.5.exe)可以在这里http://www.devexpresscn.com/下载。 2.安装DXperience控件到某一目录下。(如:D:\DevExpress 11.2) 3.下载对应的汉包。(如:DXperience-11.2.5.exe的汉化包,名称为:dxKB_A421_DXperience_v11.2_(2012-04-27).zip)可以在http://www.devexpresscn.com/news/DevExpress-news-94.html下载。 4.解压汉包,在解压后的文件夹找到DevExpress.DLL文件夹,在DevExpress.DLL文件夹找到zh-CHS文件夹。 5.把zh-CHS文件夹复制到你建立的工程下的Debug文件夹。(如 D:\Test\汉化Test\汉化Test\bin\Debug) 到这里汉化资源已经到位了。下面是在编程时使用汉化资源。(以C#为例) 6.在编程时,在Main方法加入System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS");(C#Main方法在Program.cs文件) 比较: 加入前: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 汉o化?¥Test { static class Program { /// /// 应?|用??程¨?序¨°的ì?主??入¨?口¨2点ì?。?ê /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } 加入后: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace 汉o化?¥Test { static class Program { /// /// 应?|用??程¨?序¨°的ì?主??入¨?口¨2点ì?。?ê /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-CHS"); Application.Run(new Form1()); } } } 到这汉化已经全部完成。接下来就是托控件了(使用控件)。如果你连托控件都不会,那么……我已被震亡了!! 说明:这个方法汉化不完全。(是官方汉化包的问题)

110,567

社区成员

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

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

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