I'm trying to figure out the best way to manage multiple forms in a C# application that uses dual-monitors. The application starts to a "launchpad," which just gives the operator some quick information and a "GO" button. Pressing that button hides the launchpad and displays a form on each monitor in full-screen. I've tried to capture the relevant code here:
private static List<Thread> _displays = new List<Thread>();
// "GO" button handler
private void OnClick(Object sender, EventArgs args) {
Launch(new Form1());
Launch(new Form2());
WaitForAllDisplays();
}
private static void Launch(Form form) {
Thread thread = new Thread(LaunchDisplay);
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start(form);
_displays.Add(thread);
}
private static void LaunchDisplay(Object obj) {
Form display = obj as Form;
// [snip] logic to place form on correct monitor [/snip]
display.ShowDialog();
}
public static void WaitForAllDisplays() {
foreach (Thread thread in _displays) {
thread.Join();
}
}
It feels a little messy to leave the main thread blocked on this WaitForAllDisplays()
call, but I haven't been able to think of a better way to do this. Notice that Form1
and Form2
are independent of each other and never communicate directly.
I considered using a counting semaphore to wait for all displays to close, but this is a little opposite of a traditional semaphore. Instead of executing when a resource becomes available, I want to block until all resources are returned.
Any thoughts on a better approach?