开发者

System.InvalidOperationException in vs 2005

开发者 https://www.devze.com 2023-01-06 15:31 出处:网络
I used dotnetmagic library and created a dockable window, that when run from IDE (VS2005) wor开发者_Python百科ks, but when I use the executable it raises an exception which reads:

I used dotnetmagic library and created a dockable window, that when run from IDE (VS2005) wor开发者_Python百科ks, but when I use the executable it raises an exception which reads:

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)

How to cut out these thing, can anyone help me to solve this?


You should do what you're told to do in the exception message:

Ensure that your Main function has STAThreadAttribute marked on it.

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

The reason behind this is that Windows Forms application do not support being run from a thread that is not marked to run in a single-threaded apartment and if you omit this attribute the application uses the multithreaded apartment model.

Update:

If you already marked the application entry point with the attribute and this not solved your problem then somewhere in your application you're creating threads that are executing OLE calls from a multithreaded apartment. First things first, you'll need to pinpoint these locations and then update your question with the usage scenarios so we can help you. Also make sure that you're not creating controls or interacting with them from a thread other than the main thread.


Open Program.cs and add [STAThread] on the line before void Main()

i.e.

[STAThread]
static void Main()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号