tags:

views:

50

answers:

2

Hi all,

I used dotnetmagic library and created a dockable window, that when run from IDE (VS2005) works, 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?

+4  A: 

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.

João Angelo
Already the threadAttribute is there,i even set the ApartmentState for the thread as Single.
karthik
You should not use the `ApartmentState` property because it's obsolete, use the method `SetApartmentState` instead and before starting the thread in question.
João Angelo
+2  A: 

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

i.e.

[STAThread]
static void Main()
DaveShaw