This is not a very trivial issue, but you can do it very easily.
Here is one trick: you should do it somewhere when the
System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
is already called, say, somewhere in the code of the main window. If you call it from its constructor, it will certainly work. Let's say, it is called withing the form constructor.
internal MyForm() {
if () {
} else {
MessageBox.Show("Weather Link cannot start because the module reference file is missing");
BeginInvoke(new System.Action(() => { System.Windows.Forms.Application.Exit(); }));
}
}
[EDIT]
On second though, this can work in even funnier situation, right in the entry point of the application:
using System.Windows.Forms;
void Main() {
Form mainForm =
if () {
} else {
MessageBox.Show("Weather Link cannot start [...] is missing");
mainForm.BeginInvoke(new System.Action(() => {
System.Windows.Forms.Application.Exit();
}));
}
Application.Run(mainForm);
}
[END EDIT]
Note, it should be
BeginInvoke
, not
Invoke
. Why? Why
BeginInvoke
works? I would leave it as the exercise for your homework. It will really help you to understand a lot about how event-oriented programming works. :-)
And this is one of the relatively rare example when
BeginInvoke
is useful even when no multithreading is involved!
—SA