Please suggest which method is proper(if any) for correct and efficient use of ParallelOptions, TaskCreationOptions and Task.Factory.StartNew(() =>.
private void NeedToUse_MaxDegreeOfParallelism_Method1() { CancellationTokenSource tokenFor_task = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions(); //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token; parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount; //parOpts.TaskScheduler = TaskScheduler.Default; TaskCreationOptions tco = new TaskCreationOptions(); tco = TaskCreationOptions.PreferFairness; Task task = null; task = Task.Factory.StartNew(() => { while (!tokenFor_task.IsCancellationRequested) { LongRunningMethod(); } }, tokenFor_task.Token, tco, TaskScheduler.Default); } private void NeedToUse_MaxDegreeOfParallelism_Method2() { //CancellationTokenSource tokenFor_task = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions(); parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token; parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount; parOpts.TaskScheduler = TaskScheduler.Default; TaskCreationOptions tco = new TaskCreationOptions(); tco = TaskCreationOptions.PreferFairness; Task task = null; task = Task.Factory.StartNew(() => { while (!parOpts.CancellationToken.IsCancellationRequested) { LongRunningMethod(); } }, parOpts.CancellationToken, tco, parOpts.TaskScheduler); } private void NeedToUse_MaxDegreeOfParallelism_Method3() { CancellationTokenSource tokenFor_task = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions(); //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token; parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount; //parOpts.TaskScheduler = TaskScheduler.Default; TaskCreationOptions tco = new TaskCreationOptions(); tco = TaskCreationOptions.PreferFairness; Task task = null; task = Task.Factory.StartNew(() => { Parallel.Invoke(parOpts, () => //while is already in LongRunningMethod() because can not be here //while (!tokenFor_task.IsCancellationRequested) //{ LongRunningMethod() //} ); }, tokenFor_task.Token, tco, TaskScheduler.Default); } private void NeedToUse_MaxDegreeOfParallelism_Method4() { CancellationTokenSource tokenFor_task = new CancellationTokenSource(); ParallelOptions parOpts = new ParallelOptions(); //parOpts.CancellationToken = tokenFor_task_tpl_Pair01.Token; parOpts.MaxDegreeOfParallelism = Environment.ProcessorCount; //parOpts.TaskScheduler = TaskScheduler.Default; TaskCreationOptions tco = new TaskCreationOptions(); tco = TaskCreationOptions.PreferFairness; Task task = null; Parallel.Invoke(parOpts, () => task = Task.Factory.StartNew(() => { while (!tokenFor_task.IsCancellationRequested) { LongRunningMethod(); } }, tokenFor_task.Token, tco, TaskScheduler.Default) ); } Currently I don't get any errors. First and second methods do not take into account MaxDegreeOfParallelism which I need to use. Ideally I would not use Parallel.Invoke but how to include parOpts.MaxDegreeOfParallelism in Task.Factory.StartNew?