I’ve read about advantages of Tasks Difference between Task (System.Threading.Task) and Thread
Also msdn says that “…in the .NET Framework 4, tasks are the preferred API for writing multi-threaded, asynchronous, and parallel code.”
Now my program contains such code which receive multicast data from udp:
thread = new Thread(WhileTrueFunctionToReceiveDataFromUdp);
.....
thread.Start();
I have several such threads for each socket.
Am I better to replace this code to use Task?
It depends on what you’re doing – if you’re not going to use any of the new features in
Taskand the TPL, and your existing code works, there’s no reason to change.However,
Taskhas many advantages – especially for operations that you want to run in a thread pool thread and return a result.Also – given that you’re using “threads for each socket”, you likely will have longer life threads. As such, if you do switch to
Task.Factory.StartNew, you’ll potentially want to specify that the tasks should be LongRunning or you’ll wind up using a lot of ThreadPool threads for your socket data (with the default scheduler).