My goal is to drag and drop a file out of my app into a dropzone of a website.
For that I've made this form:
public partial class Form2 : Form
{
private DataObject File;
public Form2(string filePath)
{
InitializeComponent();
File = new DataObject();
File.SetFileDropList(new StringCollection() { filePath });
labelFileName.Text = Path.GetFileName(filePath);
}
private void labelFileName_MouseDown(object sender, MouseEventArgs e)
{
labelFileName.DoDragDrop(File, DragDropEffects.Copy);
}
}
It works if I use it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var form2 = new Form2("C:\\testfile.txt");
form2.ShowDialog();
}
}
But drag and drop dosn't work if I use it in an async Task like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void buttonRunAsync_Click(object sender, EventArgs e)
{
await Task.Run(() => OpenForm2());
}
private async Task OpenForm2()
{
var form2 = new Form2("C:\\testfile.txt");
form2.ShowDialog();
// do some stuff based on dialog result
}
}
I think that's because it's not running in the UI thread but I haven't found a good explanation and also I'm not sure if that's the problem.