树结构上的并行任务未完成

时间:2016-01-26 13:39:00

标签: c# .net recursion task-parallel-library

.NET任务并行性的新手。目标是遍历树结构,其中每个分支由一个父节点,一个子节点和一个操作节点(如权重)组成。并为每个节点创建一个扩展对象并将其保存到db。我跟着可能的duplicate对话。但观察结果是树没有完全走过。这个过程会意外地提前退出。以下是我的代码:

public void InitializeScheduleVariables_Parallel(IResource ANode, double aNumRequired, double aBatchRequired, double aAcceptProbability, AppContext aAppContext, bool ARecursively = true)
    {
        var LTasks = new List<Task>();
        var LUser = aAppContext.LocalContext.User;
        LTasks.Add(Task.Factory.StartNew(() =>
        {
            var LNewContext = new AppContext(new DbContext(new Context(LUser)));
            var LNewRep = new ResourceRepository(LNewContext);
            ANode = LNewRep.Get(ANode.Id);

            ANode.ResourceInstance_Create(); // Create the ResourceInstance on the Resourse if it not already exists.
            ANode.ResourceInstance.Required = aNumRequired;
            ANode.ResourceInstance.ScheduleSource = ResourceInstance.ScheduleSourceEnum.Undefined;
            ANode.ResourceInstance.ScheduleState = ResourceInstance.ScheduleStateEnum.Unscheduled;
            ANode.ResourceInstance.ScheduleMode = ResourceInstance.ScheduleModeEnum.Undefined;
            ANode.ResourceInstance.BatchRequired = aBatchRequired;
            ANode.ResourceInstance.ProbabilityOfCompletion = aAcceptProbability;
            ANode.ResourceInstance.Save();
        }));

        if (ARecursively)
        {
            foreach (AssemblyLink LAssembly in ANode.GetOutEdges())
            {
                LTasks.Add(Task.Factory.StartNew(() =>
                {
                    // SET The Variables for the Production Operations AS WELL
                    IOperationResource LOperation = LAssembly.Operation;
                    if (LOperation != null)
                    {
                        var LNewContext = new AppContext(new DbContext(new Context(LUser)));
                        var LNewRep = new OperationResourceRepository(LNewContext);
                        LOperation = LNewRep.Get(LOperation.Id);

                        LOperation.ResourceInstance_Create(); // Create the ResourceInstance on the Resourse if it not already exists.
                        LOperation.ResourceInstance.Required = aNumRequired / LAssembly.OutputQuantity;
                        LOperation.ResourceInstance.BatchRequired = aBatchRequired / LAssembly.OutputQuantity;
                        LOperation.ResourceInstance.ScheduleSource = ResourceInstance.ScheduleSourceEnum.Undefined;
                        LOperation.ResourceInstance.ScheduleState = ResourceInstance.ScheduleStateEnum.Unscheduled;
                        LOperation.ResourceInstance.ScheduleMode = ResourceInstance.ScheduleModeEnum.Undefined;
                        LOperation.ResourceInstance.ProbabilityOfCompletion = aAcceptProbability;
                        LOperation.ResourceInstance.Save();
                    }
                }));

                LTasks.Add(Task.Factory.StartNew(() =>
                {
                    // Recursively SET Child NODES 
                    IResource LChildNode = LAssembly.Child;
                    double LNumRequired_Child = aNumRequired * LAssembly.InputQuantity / LAssembly.OutputQuantity;
                    double LNumBatchRequired_Child = LChildNode.Quantity * LAssembly.InputQuantity / LAssembly.OutputQuantity;
                    InitializeScheduleVariables_Parallel(LChildNode, LNumRequired_Child, LNumBatchRequired_Child, aAcceptProbability, aAppContext, ARecursively);
                }));
            }
        }
        Task.WaitAll(LTasks.ToArray());
    }

有人可以分享一些想法吗?谢谢。

0 个答案:

没有答案