Click here to Skip to main content
15,887,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As the title of the question says, I want to kill a thread from the same application that started it.

But... the thread is created this way:
new Thread => {...}).Start();
It works good, until I close my application and a MyApplication.exe stopped working messageBox shows because the thread has not been killed.

So, I am asking what is the correct way to close a thread started in that way.
Note: I tried using the currentThread.Abort(); and did not work.

Code Sample(of how the thread is started and what it does):
C#
private void btnListen_Click(object sender, EventArgs e)
       {
           server.Start(); // Starts Listening to Any IPAddress trying to connect to the program with port 1980
           MessageBox.Show("Waiting For Connection");
           new Thread(() => // Creates a New Thread (like a timer)
               {
                   client = server.AcceptTcpClient(); //Waits for the Client To Connect
                   MessageBox.Show("Connected To Client");
                   if (client.Connected) // If you are connected
                   {
                       ServerReceive(); //Start Receiving
                   }
               }).Start();
       }


Thanks in advance. CCB
Posted
Updated 1-Aug-15 18:57pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Aug-15 23:42pm    
It does work, and should be used with care and full understanding of what you are doing. Most people recommend cooperative thread exit, but in many cases this is lame, in others simply not acceptable.
—SA
ChrisCreateBoss 2-Aug-15 0:06am    
So what do you suggest me to do?
Sergey Alexandrovich Kryukov 2-Aug-15 0:29am    
Create as short code sample as possible, but it should be comprehensive, just enough to demonstrate only one problem. Use "Improve question" to add it.
—SA
ChrisCreateBoss 2-Aug-15 0:57am    
Done. See the code.
Sergey Alexandrovich Kryukov 2-Aug-15 7:08am    
You did not even try to exist thread method, or abort thread, or anything. To abort thread, you have to have a named thread, to call myThread.Abort from some other thread. Thread.Abort has nothing to do with unsafe Windows TerminateThread. There is a wide class of problems where you cannot do "graceful" thread termination, and a wide class of problems where "graceful" termination is just silly. :-)
—SA

1 solution

Set the Thread IsBackground[^] property to true before you start it: threads marked in this way are automatically killed by the framework when your application exits.
If you don't, they need to terminate themselves when the rest of the app closes (this is to give a "graceful" way for the thread to shut down tidily)

"Where do I set that bool in my code? See that my thread has not been named."

You're kidding, right? :laugh:

C#
Thread t = new Thread(() => // Creates a New Thread (like a timer)
    {
        client = server.AcceptTcpClient(); //Waits for the Client To Connect
        MessageBox.Show("Connected To Client");
        if (client.Connected) // If you are connected
        {
            ServerReceive(); //Start Receiving
        }
    });
t.IsBackground = true;
t.Start();
 
Share this answer
 
v2
Comments
ChrisCreateBoss 2-Aug-15 1:40am    
Where do I set that bool in my code? See that my thread has not been named.
OriginalGriff 2-Aug-15 1:54am    
Answer updated.
ChrisCreateBoss 2-Aug-15 14:40pm    
Ok. Thanks for your update. I did not name it 'cause the last try I did was like your update but instead of setting isBG to true I just called Abort(); and it did not work(don't know why, because no exceptions where thrown). So I changed back to the unnamed thread and see if I could find a way to kill it. But now I'll try with that property and see if it works. Thanks again.
OriginalGriff 2-Aug-15 14:45pm    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900