I’ve just found this gem in a form in our codebase. I think I already know the answer to this: I’m pretty sure that calling MessageBox.Show in a background thread is a terrible idea, but could someone confirm and clarify for me?
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (_FileScanner.IsSending)
{
Trace("Cannot close form because: Transferring files.");
e.Cancel = true;
Task<DialogResult> t = Task.Factory.StartNew(() => MessageBox.Show("Transferring files"));
return;
}
}
There seems to be little-to-no point in doing this and in fact potential dangers – considering the heavy work is actually being processed on non-UI-blocking threads using Tasks (as should be the case) then the very time you want the form to block input (depending on requirements, but this still stands regardless of dialog modality) until acknowledgement is when you’d do this on the STAThread.