简体   繁体   中英

Share TaskScheduler instance amongst several tasks

I created a TaskScheduler and i am passing it as an argument across two different tasks.

Is there any problem in doing it? Should i be creating a new TaskScheduler instance for each task?

Here's the sample example (actual code inside each task removed for sake of simplicity)

var uiSch = System.Threading.Tasks.TaskScheduler.FromCurrentSynchronizationContext();

var t1 = Task.Factory.StartNew<List<Carrier>>(() =>
                    {
                        //does stuff
                    })
                    .ContinueWith(previous =>
                        {
                            //does stuff
                        },
                        System.Threading.CancellationToken.None,
                        TaskContinuationOptions.OnlyOnRanToCompletion,
                        uiSch);

var t2 = Task.Factory.StartNew<List<Logic.WarehouseLogic.Warehouse>>(() =>
                    {
                        //does stuff
                    })
                    .ContinueWith(previous =>
                        {
                            //does stuff
                        },
                        System.Threading.CancellationToken.None,
                        TaskContinuationOptions.OnlyOnRanToCompletion,
                        uiSch);

[EDIT1]

My question was partially related to the following error: 'The current SynchronizationContext may not be used as a TaskScheduler' A fix can be found here

在同一个任务计划程序上计划多个任务没有问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM