Hi guys,
I have a windows forms application, with a button - on the button's event handler, I need to download a file with SaveFileDialog. But I need to do this asynchronously on a separate thread.
So far, I came up with this code, but I don't know if my approach is flawed or OK:
private void btnDownload_Click(object sender, EventArgs e)
{
ThreadStart tStart = new ThreadStart(DoWorkDownload);
Thread thread = new Thread(tStart);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
private void DoWorkDownload()
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = "C:\\";
sfd.Filter = "All files (*.*)|*.*";
sfd.FilterIndex = 1;
sfd.RestoreDirectory = true;
if (sfd.ShowDialog() == DialogResult.OK)
{
//do file saving here
}
}
}
My logic in the code above is: on button click create a new thread, pass the DoWorkDownload() method to the thread, and then start it; at that moment it is supposed to enter the work method - however, when debugging it never enters DoWorkDownload().
Does anyone know what I am missing?
Thank you.