简体   繁体   English

如何在C#中处理/实现多个任务

[英]How to handle/implement multiple Tasks in C#

I have list of items on which I have to perform big computation task, so I have Implemented the same like following. 我有必须执行大型计算任务的项目列表,因此我实现了类似以下内容的项目。 Please let me know if I am doing any thing wrong here. 如果我在这里做错了任何事情,请告诉我。

{
    "MainList": [{
            "Name": "First item from root task",
            "Task": [{
                "SubTask1": "...",
                "SubTask2": "..."
            }]
        },
        {
            "Name": "Second item from root task",
            "Task": [{
                "SubTask1": "...",
                "SubTask2": "...",
                "SubTask3": "...",
                "SubTask4": "..."
            }]
        }
    ]
}

Scenario: 场景:

  1. Have to perform big computational task on each item from list say T1. 必须从列表说T1的每个项目上执行大的计算任务。
  2. On completion of any of the task have to perform the another task on each item from same list(this have to perform after T1 gets completed) and in parallel have to perform all the sub tasks from "Tasks" property. 完成任何一项任务后,必须对同一列表中的每个项目执行另一个任务(必须在T1完成后执行),并并行执行“任务”属性中的所有子任务。

Please note, both the task from STEP 2, have to execute after first task gets completed, and both these task can then execute paralley. 请注意,STEP 2中的两个任务都必须在第一个任务完成后执行,然后这两个任务都可以执行并行处理。

Considering above scenarios, I have developed the code like below: 考虑到以上情况,我开发了如下代码:

Code: 码:

List<Task<object>> mainFirstTaskList = new List<Task<object>>();
List<Task<object>> mainSecondTaskList = new List<Task<object>>();
List<Task> subTaskList = new List<Task>();
foreach (var itm in MainList)
{
    mainFirstTaskList.Add(Task.Factory.StartNew<object>(() =>
    {
        //Use "itm" from iteration
        //Perform big computational task on each item
        return resultFirstMainList;
    }));
}
while (mainFirstTaskList.Count > 0)
{
    int finishedTask = Task.WaitAny(mainFirstTaskList.ToArray());   //waiting for any of the task to gets complete
    Task<object> t = mainFirstTaskList[finishedTask];
    var result = t.Result;

    //Perform Another Task on the same list
    mainSecondTaskList.Add(Task.Factory.StartNew<object>(() =>
    {
        //use result from first task completed
        //Perform big computational task on each item
        return resultSecondMainList;
    }));

    //Perform the task on sub item list
    subTaskList.Add(Task.Factory.StartNew<object>(() => 
    {
        //Have used Parallel.For to partition the sub task computation
        //And have added this Parallel.For inside another Task, as Parallel.For will partition the tasks on current thread
        Parallel.For(1, subItemIndex, i =>
        {
            //Perform big task computation
        });
    }));
}

Please let me know, if I have did any thing wrong here. 如果我在这里做错了什么,请告诉我。

Thanks in Advance!!! 提前致谢!!!

I think I roughly understood what you're doing. 我想我大致了解您在做什么。 You should be use Microsoft's Reactive Framework for this. 您应该为此使用Microsoft的Reactive Framework。 Then you can do this: 然后,您可以执行以下操作:

var query =
    from mainItem in MainList.ToObservable()
    from firstResult in Observable.Start(() => ProcessMainItem(mainItem))
    from secondResult in Observable.Start(() => ProcessFirstItem(firstResult))
    from subItem in secondResult.ToObservable()
    from result in Observable.Start(() => ProcessSubItem(subItem))
    select result;

IDisposable subscription =
    query
        .Subscribe(x =>
        {
            /* Process results as they are computed */
        });

Every computation is done in parallel and the results are put together in the final result. 每次计算都是并行进行的,并且将结果汇总到最终结果中。

You can even have a select new { mainItem, firstResult, secondResult, subItem, result }; 您甚至可以select new { mainItem, firstResult, secondResult, subItem, result };一个select new { mainItem, firstResult, secondResult, subItem, result }; if you want to tie all of the intermediate results together. 如果您想将所有中间结果联系在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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