Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:52:04+00:00 2026-06-10T14:52:04+00:00

The method below creates and starts a new thread for every client that is

  • 0

The method below creates and starts a new thread for every client that is connected to TCP listener.

void StartClientCommuncationThread(TcpClient Client)
{
    Thread TcpClientThread = new Thread(new ParameterizedThreadStart(HandleClientCommTask));
    TcpClientThread.Start(Client);
}

I want to abort all threads when I pressed a button but I don’t know how many threads were created and I could not reach the thread outside the method.

How can I abort the threads when I pressed a button?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:52:05+00:00Added an answer on June 10, 2026 at 2:52 pm

    Assuming you actually want to do an abort and not just end the thread, is there any reason you can’t store the thread references in a collection and then call abort() on everyone?

    The comments below are right. It would be better if you listened on a cancellation token to end a thread. However, sometimes you don’t control certain threads, lets say something is unresponsive within the thread code because of a 3rd party library. This is a good time to call abort. Keep in mind that nothing will be cleaned up properly, your finally blocks may not execute, handles that need to be disposed of won’t be disposed, etc. It’s a pretty dirty job to call abort.

    If you have access to the code running in the thread you should listen for cancellation tokens, or even just a shared (volatile) boolean value indicating that its time to bail out. If the thread has a long Thread.Sleep() in it then you should replace that with a mutex that you can pulse to wake the thread up and tell it to gracefully exit.

    With reference to the IsBackground property on a thread. MSDN says

    Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating.

    So if you need to terminate the thread while your app is running setting this property won’t do anything.

    Here is an example illustrating the point

    public class DisposeTest : IDisposable
    {
        public void Run()
        {
            while (true)
            {
                Thread.Sleep(TimeSpan.FromDays(1));
            }
    
            Dispose();
    
        }
        public void Dispose()
        {
            Console.WriteLine("disposing");
        }
    }
    
    public class TestDriver
    {
        static void Main(string[] args)
        {
            var diposeTest = new DisposeTest();
    
            var thread = new Thread(diposeTest.Run)
                                {
                                    IsBackground = true
                                };
            thread.Start();
    
            Thread.Sleep(TimeSpan.FromSeconds(1));
        }
    }
    

    In this app dispose never gets hit, even though the application didn’t hang and exited without error. This is a bad idea because you can leave sockets open, files unclosed, sql queries unfinished, all sorts of stuff.

    There are many ways to end a thread, just be mindful of how you end it because doing so with abort could cause side effects if you aren’t careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple UDP server that creates a new thread for processing incoming
I have a method that creates a thread in C# that runs a method
In ASP.NET web application a worker thread creates a non-threadpool thread like the below:
how to Create a thread that will check the song position every second and
From my main thread, I launch an image loader method method-A (below). The problem
I'm calling a WebService with HttpWebRequest.Create method and read it with StreamReader, (below method
As I created a Progress bar using below code in a on click method
The method below called UserCanAccessThisPage is based on the following logic: each user and
I am using the method below to get an array from my plist and
When the Action method below attempts to return the Json result no data object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.