C# – Creating threads – Task.Factory.StartNew vs new Thread()

.net-4.0cmultithreadingnet

I am just learning about the new Threading and Parallel libraries in .Net 4

In the past I would create a new Thread like so (as an example):

DataInThread = new Thread(new ThreadStart(ThreadProcedure));
DataInThread.IsBackground = true;
DataInThread.Start();

Now I can do:

Task t = Task.Factory.StartNew(() =>
{
   ThreadProcedure();
});

What is the difference if any?

Thanks

Best Answer

There is a big difference. Tasks are scheduled on the ThreadPool and could even be executed synchronous if appropiate.

If you have a long running background work you should specify this by using the correct Task Option.

You should prefer Task Parallel Library over explicit thread handling, as it is more optimized. Also you have more features like Continuation.