正确使用ParallelOptions,TaskCreationOptions和Task.Factory.StartNew?

as7*_*s74 4 c# task-parallel-library

请建议正确有效地使用ParallelOptions,TaskCreationOptions和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)
    );
}
Run Code Online (Sandbox Code Playgroud)

目前我没有收到任何错误.第一种和第二种方法没有考虑我需要使用的MaxDegreeOfParallelism.理想情况下,我不会使用Parallel.Invoke,但如何在Task.Factory.StartNew中包含parOpts.MaxDegreeOfParallelism?

svi*_*ick 6

你的代码和问题没有多大意义.Task.Factory.StartNew()不接受MaxDegreeOfParallelism,因为它执行单个动作.Parallel.Invoke()确实接受了这个参数,但是当你有一个动作时,使用该方法没有任何意义.

我不应该问这样一个非常具体的问题,而是应该退后一步,看看你实际想要达到的目标,然后可能会问一个新问题.

编辑:现在我想我终于理解了你要做的事情:在每个核心上,你想要执行一个单独的循环.为此,您可以使用例如Parallel.For():

Parallel.For(0, Environment.ProcessorCount, parOpts, () =>
    {
        while (!tokenFor_task.IsCancellationRequested)
        {
            LongRunningMethod();
        }
    });
Run Code Online (Sandbox Code Playgroud)