开发者

DoDragDrop freezes WinForms app sometimes

开发者 https://www.devze.com 2022-12-18 04:33 出处:网络
I\'m doing a Drag Drop to external app like this: string[] files = new string[/* */]; // get files DataObject o = new Dat开发者_StackOverflow社区aObject(DataFormats.FileDrop, files);

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

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

// get files

DataObject o = new Dat开发者_StackOverflow社区aObject(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?


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.


It will always freeze, but the delay isn't noticeable with small files. Check out this answer.

0

精彩评论

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

关注公众号