Restart Threads?

Hello All,

I am trying to work on a client/server software.

I have written a thread to manage the socket listening.

But the problem is that after the current user disconnects i terminate the thread with :-

thrdListener.Join();

or

thrdListener.Abort();

But the problem is that i cant restart the same thread again since my thread throws a ThreadStateException

Is there any work around for this

How can i restart the same thread


Answer this question

Restart Threads?

  • Lenin82

    Yes, I talk about the thread pool a bit in my threading articles (http://www.pobox.com/~skeet/csharp/threads) but I don't currently have nearly a stern enough warning against using them for anything non-trivial.

    The problem with the system threadpool is that it's shared - and there are lots of .NET methods which use the threadpool even if you don't expect them to. Now, if all the threads in the threadpool call some of those methods and then wait for something to happen, you can deadlock the threadpool. I know people who have lost weeks of development trying to track down nasty bugs like that.

    The use of a thread pool isn't a bad idea - I just wouldn't recommend the system one unless the code in it is very well understood in terms of the framework calls it makes. (For instance, I would try to avoid doing any IO in a threadpool thread, as that tends to be where the framework starts using the threadpool internally.)

    Jon


  • Aulphar

    OK... agreed to arnd 80% of what you say and thankyou for that.

    But tell me, my server apllication has the option for user to start or stop listening connections so what method should i use for the thread then

    Should i use Sleep or Suspend

    Also when you said "start a new thread, with the same ThreadStart delegate."

    Did you mean:-
    Thread newThread; //Variable declaration
    newThread = new Thread(new ThreadStart(myThreadProc)); //Variable Initialization

    -----------------

    The above Variable Initialization line should be called again and again whenever i want to restart the thread

  • needhelp1111

    Can anyone of you guide me for C# Remoting..

    Any tutorials/links/books

    Because i am thinking Remoting will solve all my hassel

  • David Paynter

    Also remember that you already have the threadpool, waiting for you to grab and use its threads!

    Creating and destroying threads is generally expensive in terms of performance, so use the ones running in the threadpool. I know Jon has something on this in his threading articles, so check that out for more info.

    -e



  • Quintin Willison

    You have exited the thread, so it is dead, and cannot be restarted. To do the same work you'd have to start a new thread, with the same ThreadStart delegate.

    As an aside, generally in a threaded socket server, you have one thread listening for connections which hands connections off to other threads for processing. The listener thread only stops when the service/application is stopped.

    In any case, it is generally much easier to just use Remoting to handle your client/server communications. It will handle the listening for you, as well as managing the threading if you use SingleCall. Performance-wise it's surprisingly fast, even when compared to a highly optimised asychronous socket server.



  • Dimitry

    Don't use Sleep or Suspend for this. (In fact, don't use Suspend at all.) Stop the thread (gracefully - see http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml) when the user clicks Stop, and if they click Start, just start a new thread in the same way that you did initially.

    Jon


  • Restart Threads?