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

解决方案 »

  1.   

    Single Thread Apartment
      

  2.   

    Why is STAThread required?
     it changes the apartment state of the current thread to be single threadedhttp://blogs.msdn.com/jfoscoding/archive/2005/04/07/406341.aspx
      

  3.   

    > 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.
      

  4.   

    关于 COM的书有 介绍
    我对它的认识也很模糊
      

  5.   

    在C#中,默认的就是,[STAThread].我要做socket通信程序,需要服务器端与多个客户端通信.在服务器端再用这个[STAThread]是不是就不行了吧?
      

  6.   

    “/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 更新 产生这样一个异常 到底是为什么? 请高人帮忙~
     
      

  7.   

    不是,你可以看看CheckForIllegalCrossThreadCalls属性相关的帮助!可能有点帮助!