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. Multi-Threading Question

Multi-Threading Question

Scheduled Pinned Locked Moved C#
questioncsharpcomsysadmintools
11 Posts 4 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.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, I have created a multi-threaded TCP/IP server. Each client connection runs in it's own thread. The problem that I have it when I close the server and there are still clients connected. The thing is that the client threads remain active. How can stop / dispose of the client threads so that the server can be closed for good? Many thanks in advance Kind regards,

    The only programmers that are better C# programmers, are those who look like this -> :bob:

    :)Programm3r My Blog: ^_^

    M L 2 Replies Last reply
    0
    • P Programm3r

      Hi all, I have created a multi-threaded TCP/IP server. Each client connection runs in it's own thread. The problem that I have it when I close the server and there are still clients connected. The thing is that the client threads remain active. How can stop / dispose of the client threads so that the server can be closed for good? Many thanks in advance Kind regards,

      The only programmers that are better C# programmers, are those who look like this -> :bob:

      :)Programm3r My Blog: ^_^

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      well you need to handle connections when you close down the server. You should have a list of all connected clients, right? so you will want to stop incomming connections. then you can abort all client threads, preferable after sending information to the client about the shut down

      Life goes very fast. Tomorrow, today is already yesterday.

      P 1 Reply Last reply
      0
      • M musefan

        well you need to handle connections when you close down the server. You should have a list of all connected clients, right? so you will want to stop incomming connections. then you can abort all client threads, preferable after sending information to the client about the shut down

        Life goes very fast. Tomorrow, today is already yesterday.

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        musefan wrote:

        You should have a list of all connected clients, right?

        Yes.

        musefan wrote:

        then you can abort all client threads

        The only thing is that the client threads spawn out of another thread that is listening for any incoming connections. If a new connection is accepted by the listen thread the listen thread spawns a new thread for that connections. Thus I do not have the handle to each of the client threads that have been spawned, well I think so anyway. Many thanks for your response Kind regards,

        The only programmers that are better C# programmers, are those who look like this -> :bob:

        :)Programm3r My Blog: ^_^

        U M 2 Replies Last reply
        0
        • P Programm3r

          musefan wrote:

          You should have a list of all connected clients, right?

          Yes.

          musefan wrote:

          then you can abort all client threads

          The only thing is that the client threads spawn out of another thread that is listening for any incoming connections. If a new connection is accepted by the listen thread the listen thread spawns a new thread for that connections. Thus I do not have the handle to each of the client threads that have been spawned, well I think so anyway. Many thanks for your response Kind regards,

          The only programmers that are better C# programmers, are those who look like this -> :bob:

          :)Programm3r My Blog: ^_^

          U Offline
          U Offline
          User 3491102
          wrote on last edited by
          #4

          Did you use Socket.beginAccept?

          1 Reply Last reply
          0
          • P Programm3r

            musefan wrote:

            You should have a list of all connected clients, right?

            Yes.

            musefan wrote:

            then you can abort all client threads

            The only thing is that the client threads spawn out of another thread that is listening for any incoming connections. If a new connection is accepted by the listen thread the listen thread spawns a new thread for that connections. Thus I do not have the handle to each of the client threads that have been spawned, well I think so anyway. Many thanks for your response Kind regards,

            The only programmers that are better C# programmers, are those who look like this -> :bob:

            :)Programm3r My Blog: ^_^

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            well you may want to change it so you store you connections somewhere you can access. you could try using a static collection of threads. that way they can be accessed from both your connection handling thread and you main thread.

            Life goes very fast. Tomorrow, today is already yesterday.

            P 1 Reply Last reply
            0
            • M musefan

              well you may want to change it so you store you connections somewhere you can access. you could try using a static collection of threads. that way they can be accessed from both your connection handling thread and you main thread.

              Life goes very fast. Tomorrow, today is already yesterday.

              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #6

              Mmmm .... yes I think that can do the trick. Is it possible to terminate a thread if one has the thread handle? Thanks again Regards,

              The only programmers that are better C# programmers, are those who look like this -> :bob:

              :)Programm3r My Blog: ^_^

              M 1 Reply Last reply
              0
              • P Programm3r

                Mmmm .... yes I think that can do the trick. Is it possible to terminate a thread if one has the thread handle? Thanks again Regards,

                The only programmers that are better C# programmers, are those who look like this -> :bob:

                :)Programm3r My Blog: ^_^

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                I assuming its system.Threading.Thread, you can use .Abort to end the thread.

                Life goes very fast. Tomorrow, today is already yesterday.

                P 1 Reply Last reply
                0
                • P Programm3r

                  Hi all, I have created a multi-threaded TCP/IP server. Each client connection runs in it's own thread. The problem that I have it when I close the server and there are still clients connected. The thing is that the client threads remain active. How can stop / dispose of the client threads so that the server can be closed for good? Many thanks in advance Kind regards,

                  The only programmers that are better C# programmers, are those who look like this -> :bob:

                  :)Programm3r My Blog: ^_^

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Hi, if all you need is for the server to be able to stop whenever it wants to, turn all the client-serving threads into background threads by setting their IsBackground property true. FYI: ThreadPool threads and BackgroundWorkers always are background threads, so they can't prevent your app from exiting. however if you need to communicate with the clients about the imminent shutdown, obviously you would need to keep a list and provide the appropriate code. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                  P 1 Reply Last reply
                  0
                  • M musefan

                    I assuming its system.Threading.Thread, you can use .Abort to end the thread.

                    Life goes very fast. Tomorrow, today is already yesterday.

                    P Offline
                    P Offline
                    Programm3r
                    wrote on last edited by
                    #9

                    musefan wrote:

                    I assuming its system.Threading.Thread

                    Yes. Thanks

                    The only programmers that are better C# programmers, are those who look like this -> :bob:

                    :)Programm3r My Blog: ^_^

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, if all you need is for the server to be able to stop whenever it wants to, turn all the client-serving threads into background threads by setting their IsBackground property true. FYI: ThreadPool threads and BackgroundWorkers always are background threads, so they can't prevent your app from exiting. however if you need to communicate with the clients about the imminent shutdown, obviously you would need to keep a list and provide the appropriate code. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                      P Offline
                      P Offline
                      Programm3r
                      wrote on last edited by
                      #10

                      Luc Pattyn wrote:

                      threads by setting their IsBackground property true. FYI: ThreadPool threads and BackgroundWorkers always are background threads, so they can't prevent your app from exiting.

                      Thank you for the information. This has helped a lot. Kind regards,

                      The only programmers that are better C# programmers, are those who look like this -> :bob:

                      :)Programm3r My Blog: ^_^

                      L 1 Reply Last reply
                      0
                      • P Programm3r

                        Luc Pattyn wrote:

                        threads by setting their IsBackground property true. FYI: ThreadPool threads and BackgroundWorkers always are background threads, so they can't prevent your app from exiting.

                        Thank you for the information. This has helped a lot. Kind regards,

                        The only programmers that are better C# programmers, are those who look like this -> :bob:

                        :)Programm3r My Blog: ^_^

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        You're welcome. PS: I'd rather not look like this: :bob: :)

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


                        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