Mittwoch, 23. Oktober 2013

DragDrop registration did not succeed

DeutschEnglish
Mein C# Programm hat einen Laufzeitfehler mit folgendem Text verusacht:My C# program caused an exception with the following message:
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 DevExpress.XtraTreeList.TreeList.OnHandleCreated(EventArgs e)
   at System.Windows.Forms.Control.WmCreate(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.XtraEditors.Container.EditorContainer.WndProc(Message& m)
   at DevExpress.XtraTreeList.TreeList.WndProc(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)
Ich habe eine Weile gebraucht, aber schließlich die Fehlerursache gefunden:It took me some time to find the reason for this:
Das Programm hat in einem BackgroundWorker Controls instanziiert und deren Handle erzeugt. Da BackgroundWorker IMMER im MTA Modus arbeitet, kommt es zu dieser Exception.The program used a BackgroundWorker to instantiate controls. The thread of BackgroundWorker is always in MTA, leading to the exception.
Was tun?How to fix it?
BackgroundWorker muss durch einen richtigen Thread ersetzt werden, denn an diesem Thread kann man noch vor dem Ausführen via SetApartmentState auf STA umschwenken.BackgroundWorker needs to be replaced by a real Thread, you can then use SetApartmentState to switch to STA.

Keine Kommentare:

Kommentar veröffentlichen