DoDragDrop () from another thread

Every time I want a user to be able to drag a control, I call DoDragDrop of that control.

Drag and drop works fine, but I have a problem with things:

  • DoDragDrop completely blocks the form, timer events do not skip, messages with paint are processed.

  • DoDragDrop not only blocks the drag operation, but also until the target program finishes the drop event (IE error code explorer.exe suck). Depending on the other program code, sucks.

I thought to call DoDragDrop from a new thread.

tried this:

Thread dragThread = new Thread(() => { Form frm = new Form(); frm.DoDragDrop("data", DragDropEffects.All); }); dragThread.SetApartmentState(ApartmentState.STA); dragThread.IsBackground = true; dragThread.Start(); 

but it does not work. I mean: when executing DoDragDrop from another thread like this, other controls in my program or other programs do not receive drag and drop messages.

Any other solutions?

0
multithreading c # winforms drag-and-drop blocking
source share
3 answers

The DoDragDrop method stops event processing until the first mouse event (for example, mouse movement). Therefore, the workaround I found is very simple - you just need to simulate a mouse event with the same mouse position before calling DoDragDrop:

 void XYZControl_MouseDown(object sender, MouseEventArgs e) { var senderControl = (Control) sender; ... Cursor.Position = senderControl.PointToScreen(new Point(eX, eY)); // Workaround! if (DoDragDrop(senderControl, DragDropEffects.Move) == DragDropEffects.Move) { ... } .... } 
+4
source share

You need to forget about using a stream that will send D + D notifications to windows created in this stream. Which will not be your control.

I can't do much with the "code is sucks" diagnostic. The DoDragDrop () call itself is indeed blocked until the mouse button is released. Another message loop, internal to the COM code, will receive and deliver Windows messages. Timer and paint messages should be delivered as usual. Diagnosing is very difficult until you publish any reproducible code.

+1
source share

You probably want DoDragDrop to DoDragDrop down and do the job asynchronously.

Here is the answer.

+1
source share

All Articles