How to get maximum degree of parallelism for task parallel library usage?

c#, multithreading, parallel-processing, task-parallel-library

Solution

Parallel tasks are basically threads that can be shared. Because the number of active threads is limited by the number of logical processor cores that are available, a good guess would be to just take the number of logical cores available to the program.

Environment.ProcessorCount

Problem

I want to use Parallel.invoke. If i assign 20 parallel task, only 8 of then are run concurrently. My CPU is http://ark.intel.com/products/47925 and the reported number of threads is 8. I assume number of task can be run in parallel is related to the cpu number of threads. I dont want to create more task than the number of threads. How do i know the number of threads in c#? I tried query ParallelOptions.MaxDegreeOfParallelism and all i get is -1.

Original source