0

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.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55
  • A Task is just a wrapper over a thread – Mox Jan 09 '17 at 05:38
  • 4
    @Mox, not necessarily, you can have Tasks for asynchronous code that doesn't involve threads at all. – Saeb Amini Jan 09 '17 at 05:44
  • @SaebAmini, so how do u run async code without thread? – Mox Jan 09 '17 at 05:45
  • 2
    @Mox An example is when you have IO bound asynchronous code, everything is on the same thread, it's just not synchronous. [There Is No Thread](http://blog.stephencleary.com/2013/11/there-is-no-thread.html) is a good post on the subject if you're interested in more details. – Saeb Amini Jan 09 '17 at 05:48
  • @SaebAmini, oh ok. thx =) – Mox Jan 09 '17 at 05:48
  • If you have a long running action you want to have asynchron Task.Run with the LongRunning flag is a good way to go. Internally this will be executed inside a thread but not affecting the thread pool. So you have your loved thread inside but you can handle it as all other tasks – Sir Rufo Jan 09 '17 at 22:05
  • @SirRufo - For Reliability, is the Task susceptible to the same blocking calls as the ThreadPool? Does a Task Run out of Threads available? And most important, for mission critical processes, is a Thread or a Task more reliable? – Rusty Nail Jan 09 '17 at 22:12
  • Many read only part of my question and mark it as duplicate or whatever, is that called a Comprehension Issue? – Rusty Nail Jan 09 '17 at 22:15
  • 1
    https://referencesource.microsoft.com/#mscorlib/system/threading/Tasks/ThreadPoolTaskScheduler.cs -> QueueTask method – Sir Rufo Jan 09 '17 at 22:16
  • 1
    If you cannot create a long running task you will also cannot create a thread - see the source and you will know it is the same - except you can handle it like any other task with all the whistles (async/await, ...). I only work with tasks and I do not have to create threads ... that will do the task library for me – Sir Rufo Jan 09 '17 at 22:20
  • @SirRufo - await has known issues with long running tasks. See: http://blog.i3arnon.com/2015/07/02/task-run-long-running/ for just one example. – Rusty Nail Jan 09 '17 at 22:21
  • Ah, you now all this facts I gave you ... so whats the reason for the question? – Sir Rufo Jan 09 '17 at 22:29
  • @SirRufo - Yes thank you, I gave you a thumbs up for the link. I am looking for Best Practice/Reliability for Mission Critical Work. – Rusty Nail Jan 09 '17 at 22:32

0 Answers0