Keeping forms open after the main form is closed

  • Thread starter Thread starter stumorgan
  • Start date Start date
S

stumorgan

There is probably an extremely simple answer to this question and I'm
just being foolish.

I have a main form (let's say FormMain) which opens other forms (let's
say Form1, Form2). How do I keep those created forms, Form1 and Form2,
from closing when I close FormMain? Right now what I'm doing is
threading them by wrapping the Show method and in that threaded wrapper
I have a while loop that just does Application.DoEvents() as long as
the form hasn't been closed.

private void ShowForm() // this is the threaded function
this.Show();
while(!closed)
{
Application.DoEvents();
}
}

and in the form's Closed event I set closed = true.

It works just fine, but I feel like it's probably inefficient and I
think there has to be a better (and probably simpler) way of doing it.
Any ideas? Feel free to mock me for my stupidity.
 
You probably have your FormMain as your start up object. When you
close that form, it closes the app.

Try starting your app from a Sub Main and then call Application.Run
without any arguments. The caveat to this is that when you need to
exit the app, you need to call Application.ExitThread.
 
Yes I was reading about that on another forum. How am I supposed to
know when all of the forms have been closed and that I need to call
Application.ExitThread()?

Just spitballing here, but I could pass a reference to the main program
as a parameter when I create the forms, and then have them tell the
main program when they are closing. Main program keeps a counter on
the number of open forms and once it goes down to 0 calls
Application.ExitThread().

Does that make any sense or is there a better / simpler way of doing
it? As you can tell I'm trying to condition myself to program
efficiently... Help is much appreciated.
 
Hi stumorgan,
what Chris and yourself are saying is an efficient way to code, a simple
example of this could be:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication5
{
namespace WindowsApplication5
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Create main message pump
Application.Run(new Form1());
}
}
}

public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
//Show other forms in seperate thread
Thread t = new Thread(new ThreadStart(ShowForms));
t.IsBackground = false;
t.Start();

//Close the main form - this will exit the main
//UI thread message pump, but we will have another one
this.Close();
}

/// <summary>
/// Keeps track of the number of open forms
/// </summary>
int _formCount = 0;

private void ShowForms()
{
Form f1 = new Form();
Form f2 = new Form();

//Add event handler to decrement count on close
f1.FormClosed += new FormClosedEventHandler(ChildFormClosed);
f2.FormClosed += new FormClosedEventHandler(ChildFormClosed);

//store how mant forms there are open
_formCount = 2;

//show both the forms
f1.Show();
f2.Show();

//Create a new message pump outside of the main
//UI thread message pump
Application.Run();
}

void ChildFormClosed(object sender, FormClosedEventArgs e)
{
_formCount--;

//if no open forms - kill message pump
if (_formCount == 0)
{
//Tell the current thread to exit it message pump
Application.ExitThread();
}
}


}
}


Mark Dawson
http://www.markdawson.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top