Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What's the better way to terminate the following thread?

What's the better way to terminate the following thread?

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Daniel Strigl
    wrote on last edited by
    #1

    What's the better way to terminate the following thread? 1. Method:

    ...
    public void WaitForClient()
    {
    while (true)
    {
    try
    {
    Socket s = listener.AcceptClient(); // this blocks!
    ...
    // do something!
    ...
    }
    catch (Exception)
    {
    break;
    }
    }
    }
    ...
    static void Main(string[] args)
    {
    listener = new TcpListener(8002);
    listener.Start();
    ...
    thread = new Thread(new ThreadStart(WaitForClient));
    thread.IsBackground = true;
    thread.Start();
    ...
    // do something!
    ...
    listener.Stop(); // stop listening!
    thread.Join(); // wait until the thread is terminated!
    ...
    }

    2. Method:

    ...
    public void WaitForClient()
    {
    while (true)
    {
    Socket s = listener.AcceptClient(); // this blocks!
    ...
    // do something!
    ...
    }
    }
    ...
    static void Main(string[] args)
    {
    listener = new TcpListener(8002);
    listener.Start();
    ...
    thread = new Thread(new ThreadStart(WaitForClient));
    thread.IsBackground = true;
    thread.Start();
    ...
    // do something!
    ...
    thread.Abort(); // abort the thread!
    listener.Stop(); // stop listening!
    ...
    }

    Daniel ;) --------------------------- Never change a running system!

    T 1 Reply Last reply
    0
    • D Daniel Strigl

      What's the better way to terminate the following thread? 1. Method:

      ...
      public void WaitForClient()
      {
      while (true)
      {
      try
      {
      Socket s = listener.AcceptClient(); // this blocks!
      ...
      // do something!
      ...
      }
      catch (Exception)
      {
      break;
      }
      }
      }
      ...
      static void Main(string[] args)
      {
      listener = new TcpListener(8002);
      listener.Start();
      ...
      thread = new Thread(new ThreadStart(WaitForClient));
      thread.IsBackground = true;
      thread.Start();
      ...
      // do something!
      ...
      listener.Stop(); // stop listening!
      thread.Join(); // wait until the thread is terminated!
      ...
      }

      2. Method:

      ...
      public void WaitForClient()
      {
      while (true)
      {
      Socket s = listener.AcceptClient(); // this blocks!
      ...
      // do something!
      ...
      }
      }
      ...
      static void Main(string[] args)
      {
      listener = new TcpListener(8002);
      listener.Start();
      ...
      thread = new Thread(new ThreadStart(WaitForClient));
      thread.IsBackground = true;
      thread.Start();
      ...
      // do something!
      ...
      thread.Abort(); // abort the thread!
      listener.Stop(); // stop listening!
      ...
      }

      Daniel ;) --------------------------- Never change a running system!

      T Offline
      T Offline
      Tom Welch
      wrote on last edited by
      #2

      I would suggest a third method. Use a variable to flag the thread that it needs to stop working. Then in your thread (presumably in a loop) check for the flag at regular intervals and bail out cleanly. This is probably better handled by an Event object, but I haven't used them in C# as of yet. In a simple scenario a flag variable should do.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups