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
"st*******@gmail.com" wrote:
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.