开发者

C#/.NET: how do I close the application from a parent window?

I have a Windows application that has 2 Forms. From one form I am opening a 2nd form. When opening the 2nd form I am hiding 1 form and in the second foem I am starting a thread. Now I want to close the application. But I was not able to do that.

On my 1st form I have tried:

Form1 frm = new Form1(this, tcpClient);
frm.ShowDialog();
this.Close();
Application.Exit();

But the application is not ending. It still running.

Any idea how to stop that?

EDIT (CODE Included):

On 1st form's button click event:

this.Hide();
Form1 frm = new Form1(this, tcpClient, serverMsg);
frm.Show();

On 1st form's button FormClosed event:

MessageBox.Show("Before");
Application.Exit();

On 2nd form's load event I am calling a method startThread(); on this method

ilThre开发者_JAVA技巧ad = new Thread(incomingListener);
ilThread.IsBackground = true;
ilThread.Start();


When you do frm.ShowDialog() you're opening a modal window. The code will stop at that point until you specify a DialogResult on frm. I.e. frm.DialogResult = DialogResult.Ok;.

If you use frm.Show() that might work for you.


Edit- See @GenericTypeTea's answer first. If after doing that you are still having issues see mine:

From what you describe it sounds like you have left the thread running. Have you tried creating the thread as a background thread or making sure you end that thread in a clean way?


You probably didn't set the Thread you started as a background thread. Background threads are stopped automatically when application closes. Foreground threads will keep it alive.

To set thread as background thread use IsBackground property.

See Thread.IsBackground.

Otherwise you should take care yourself of stopping the started thread before exiting the application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜