2010-07-06 70 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()