Using various methods of staring and managing Work on another Thread can be a difficult decision to make. I have tried several ways and am not yet decided on the best.
An example, Running a SSL TCP Server, lets say the code is something like the MS Example: https://msdn.microsoft.com/en-us/library/system.net.security.sslstream(v=vs.110).aspx
Using Code to run the Server on a Thread like so results in a lot of issues:
ThreadPool.QueueUserWorkItem((s) => RunSSLTCPServer(Machine01, 8080));
Tests show that sooner or later, the Thread runs into instability's, perhaps because of the Limited Thread Pool, and to many Blocking Calls.
So, what is the best way to Start the Server under another Thread?
ThreadStart StartOptions = new ThreadStart(RunSSLTCPServer);
SSLTCPThread = new Thread(StartOptions);
SSLTCPThread .Start(Machine01, 8080);
or
SSLTCPThread = new Thread(() => RunSSLTCPServer(Machine01, 8080));
SSLTCPThread.Start();
Why would one choose to start this as a Task instead?
Task SSLTCPTask = Task.Run( () => { RunSSLTCPServer(Machine01, 8080) }, TaskCreationOptions.LongRunning );
What is Best Practice and most reliable over all? I am specifically looking at Reliability of the Work being done not an explanation of the differences between a Task and a Thread.