Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DoDragDrop freezes WinForms app sometimes

I'm doing a Drag Drop to external app like this:

string[] files = new string[/* */];

// get files

DataObject o = new DataObject(DataFormats.FileDrop, files);
DoDragDrop(o, DragDropEffects.Copy | DragDropEffects.Move);

and some apps will take the files and move on to process them and my app is free to do it's stuff, but some apps will make MY app freeze until it processess all the files... is there any way I can go around that? I've tried to do it in a Thread but that didn't work so well - it didn't work at all... so, any suggestions how to make this code do not hang my app?

like image 923
argh Avatar asked Jan 28 '10 15:01

argh


1 Answers

the .NET Control.DoDragDrop is just a wapper around the Win32 api called.. (wait for it).. DoDragDrop

So it has the same limitations. DoDragDrop can only be called from a thread that has called OleInitialize, which makes the thread a STA (Single Threaded Apartment) type thread.

Because it can only be used by an STA thread, and the API design is synchronous, you are at the mercy of the destination applications' handling of drop notifications. This is just part of the design of OLE Drag and Drop and cant be changed. (OLE Drag and drop was actually designed in the Windows 3x days, before threads even existed in windows applications).

So, you could maybe make this work on another thread IFF that thread is a STA thread. You would also probably have to use interop to call the unmanaged DoDragDrop function.

like image 87
John Knoeller Avatar answered Oct 04 '22 21:10

John Knoeller