I’ve been confused by this problem for a very long time. I start the new thread by using System.Threading, just like this:
ParameterizedThreadStart threadFileExport = FileExport;
Thread threadExport = new Thread(threadFileExport)
{
IsBackground = true,
Name = "threadExport",
Priority = ThreadPriority.AboveNormal
};
threadExport.Start(_dataTable);
and
public void FileExport(object objTable)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
DefaultExt = "xlsx",
Filter = "Excel 2007-2010|*.xlsx|" +
"Excel95,97,2003|*.xls|",
FileName = "table.xlsx",
Title = "Save as. . ."
};
saveFileDialog.ShowDialog();
}
But the dialog won’t show and it seems that the thread will abort immediately when executing “ShowDialog”. Is this a bug or I made a mistake? Can a background thread shows a savefile dialog?
No, a dialog must be shown on the UI thread, just as any other UI operation.