2010-07-06 57 views
-2

我用dotnetmagic庫,並創建了一個可停靠的窗口,從IDE (VS2005)運行時的作品,但是當我使用的可執行程序,它拋出一個異常曰:System.InvalidOperationException在VS 2005

System.InvalidOperationException: DragDrop registration did not succeed. ---> 
System.Threading.ThreadStateException: Current thread must be set to single 
    thread apartment (STA) mode before OLE calls can be made. Ensure that 
    your Main function has STAThreadAttribute marked on it. 
    at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept) 
    --- End of inner exception stack trace --- 
    at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept) 
    at System.Windows.Forms.Control.OnHandleCreated(EventArgs e) 
    at System.Windows.Forms.Control.WmCreate(Message& m) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.ScrollableControl.WndProc(Message& m) 
    at System.Windows.Forms.ContainerControl.WndProc(Message& m) 
    at System.Windows.Forms.UserControl.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(
     IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

如何去掉這些東西,誰能幫我解決這個問題?

+2

-1,如果你讀了這是給你的很有幫助消息你可能會自己解決這個問題 – 2010-07-06 11:57:31

回答

4

你應該做你告訴異常消息做什麼:

確保您的主要功能有 請將STAThreadAttribute標記就可以了。

[STAThread] 
static void Main() 
{ 
    // Code to run the application 
} 

這背後的原因是,Windows窗體應用程序不支持從沒有標記爲在單線程單元運行一個線程中運行,如果你省略此屬性的應用程序使用多線程公寓模型。

更新:

如果你已經標有屬性的應用程序入口點,這在你創建正在執行的OLE從多線程調用公寓線程應用程序沒有解決你的問題,然後地方。首先,您需要精確定位這些位置,然後使用使用場景更新您的問題,以便我們爲您提供幫助。還要確保你沒有創建控件,也沒有從主線程以外的線程與它們交互。

+0

已經是threadAttribute了,我甚至把線程​​的ApartmentState設置爲Single。 – karthik 2010-07-06 11:59:47

+0

您不應該使用'ApartmentState'屬性,因爲它已經過時,請在啓動相關線程之前使用方法SetApartmentState。 – 2010-07-06 12:08:04

2

打開Program.cs的和無效的主要前行()添加[STAThread]

[STAThread] 
static void Main()