tags:

views:

235

answers:

3

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

ilThread = new Thread(incomingListener);
ilThread.IsBackground = true;
ilThread.Start();
+1  A: 

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?

RichardOD
A: 

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.

Majkel
I have set ThreadObj.IsBackground = true;but same result.
openidsujoy
+4  A: 

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.

GenericTypeTea
+1. Well spotted. If @openidsujoy fixes this issue, then the next one will may be the threading (as per my answer)!
RichardOD
Cheers. I'll be sure to return the +1 should he post anything further.
GenericTypeTea
if I used "frm.Show()" then it immediately close the entire application.
openidsujoy
I want to close the Application on second Form's closing event.
openidsujoy
You need to show us some more code, i.e. what happens on your closing event, what happens when you set your thread up. Help us help you!
GenericTypeTea