1

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.
  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.

Please note, both the task from STEP 2, have to execute after first task gets completed, and both these task can then execute paralley.

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!!!

1 Answer 1

1

I think I roughly understood what you're doing. You should be use Microsoft's Reactive Framework for this. 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 }; if you want to tie all of the intermediate results together.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks @Enigmativity, I am very new to Microsoft's Reactive, and expectation is to first to start "mainFirstTaskList" task and as task gets completed one by one have to start "mainSecondTaskList" and "subTaskList" in parallel as they are independent to each. In "subTaskList" as there are more than one task I have used Parallel.For. What you have written that all task will start in parallel but both "mainSecondTaskList" and "subTaskList" are dependent on result from "mainFirstTaskList". And is there any problem in the implementation I have?
@GaneshChoudhari - You would need to provide a minimal reproducible example for me to comment on whether or not your code works. It's hard to fully understand partial code. You should try my approach - you'll be surprise how well it handles the multi-threading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.