Multi-Threading Question
-
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: ^_^
-
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: ^_^
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.
-
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.
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: ^_^
-
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: ^_^
Did you use Socket.beginAccept?
-
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: ^_^
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.
-
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.
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: ^_^
-
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: ^_^
-
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: ^_^
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
-
I assuming its system.Threading.Thread, you can use .Abort to end the thread.
Life goes very fast. Tomorrow, today is already yesterday.
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: ^_^
-
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
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: ^_^
-
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: ^_^
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