I have what I believe to be a fairly well structured .NET 3.5 forms application (Unit Tests, Dependency Injection, SoC, the forms simply relay input and display output and don't do any logic, yadda yadda) I am just missing the winforms knowledge for how to get this bit to work.
When a connection to the database is lost - a frequent occurrence - I am detecting and handling it and would like a modal form to pop up, blocking use of the application until the connection is re-established. I am not 100% sure how to do that since I am not waiting for user input, rather I am polling the database using a timer.
My attempt was to design a form with a label on it and to do this:
partial class MySustainedDialog : Form {
public MySustainedDialog(string msg) {
InitializeComponent();
lbMessage.Text = msg;
}
public new void Show() {
base.ShowDialog();
}
public new void Hide() {
this.Close();
}
}
public class MyNoConnectionDialog : INoConnectionDialog {
private FakeSustainedDialog _dialog;
public void Show() {
var w = new BackgroundWorker();
w.DoWork += delegate {
_dialog = new MySustainedDialog("Connection Lost");
_dialog.Show();
};
w.RunWorkerAsync();
}
public void Hide() {
_dialog.Close();
}
}
This doesn't work since _dialog.Close() is a cross-thread call. I've been able to find information on how to resolve this issue within a windows form but not in a situation like this one where you need to create the form itself.
Can someone give me some advice how to achieve what I am trying to do?
EDIT: Please note, I only tried Background worker for lack of other ideas because I'm not tremendously familiar with how threading for the UI works so I am completely open to suggestions. I should also note that I do not want to close the form they are working on currently, I just want this to appear on top of it. Like an OK/Cancel dialog box but which I can open and close programmatically (and I need control over what it looks like to )