How to make DragDrop.DoDragDrop async?

Viewed 660

I use DragDrop.DoDragDrop to copy files from my C# WPF application to Windows explorer. However, the actual copying of the files causes my WPF application to freeze until the copying is complete. This copy operation could last a few seconds to several minutes.

DataObject dragdata = new DataObject(DataFormats.FileDrop);
System.Collections.Specialized.StringCollection sc = new System.Collections.Specialized.StringCollection();

//fill the StringCollection with filenames

dragdata.SetFileDropList(sc);
DragDrop.DoDragDrop(listviewitem, dragdata, DragDropEffects.Copy);

How can I call the DoDragDrop method without freezing/locking my application? I have not been able to make sense of the reported 'async' implementation of DataObject.

1 Answers

Instead of trying to make the 'DoDragDrop' method perform asynchronously, it is better to perform the copying of file on a different thread instead, as this will allow your main thread, which is running the WPF application's UI, to continue to run (No freezing) while another thread is performing the heavy I/O operation of copying the file. Once the other thread completed, you can notify your main thread that it has finished to display a status message to say that file copying has completed.

Related