Calling Dialog from Thread
-
I am developing a windows application in which I am calling a dialog window in a thread. When I minimize the Dialog, it not only minimize but also closes. Why it is showing this kind of behaviour? The code i used is: On Form1: private void button1_Click(object sender, System.EventArgs e) { Thread td = new Thread(new ThreadStart(initiateForm2)); td.Start(); } private void initiateForm2() { Demo frm2 = new Demo(); this.Hide(); frm2.ShowDialog(); } On Form2: private void btnMinimize_Click(object sender, System.EventArgs e) { this.Hide(); } The problem is that when I hide the second form, it also call the Closing event of the Dialog form. It closes the dialog window. Why it is so?
Thanks, Sandeep S. Sekhon
-
I am developing a windows application in which I am calling a dialog window in a thread. When I minimize the Dialog, it not only minimize but also closes. Why it is showing this kind of behaviour? The code i used is: On Form1: private void button1_Click(object sender, System.EventArgs e) { Thread td = new Thread(new ThreadStart(initiateForm2)); td.Start(); } private void initiateForm2() { Demo frm2 = new Demo(); this.Hide(); frm2.ShowDialog(); } On Form2: private void btnMinimize_Click(object sender, System.EventArgs e) { this.Hide(); } The problem is that when I hide the second form, it also call the Closing event of the Dialog form. It closes the dialog window. Why it is so?
Thanks, Sandeep S. Sekhon
I guess this behaviour is caused by the fact that you show the second form as modal dialog. Use the
Form.Show
method to display the second form. Furthermore you should no longer need to do this inside an extra thread, so the whole thing cuts down to:private void button1_Click(object sender, System.EventArgs e)
{
Demo frm2 = new Demo();
this.Hide();
frm2.Show();
}
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook