【问题标题】:TPL Task return valuesTPL 任务返回值
【发布时间】:2011-07-23 01:14:04
【问题描述】:

我申请了人脸识别系统 我在 30 左右收到了来自相机帧:50 帧/秒 这取决于相机类型,对于每个框架,我都有另一个功能可以让所有人都在上面 并且从当前框架中所有人的每个人中,我检查这个人是否存在于数据库中

我用简单的 C# 语句重写我之前的问题更清楚

// Get All frame from Camera you can consider it's like you are inside for loop
for (int frame = 1; frame < 50; frame++)
{
    // each fram i get the person insied this frame
    // so i get List<Persons>
    foreach (var perosn in allPersons_inFrame)
    {    
        // for each person i need to check 
        //against all my database recored
        foreach (var recored in Database)
        {
            // perosn Exist in Database
            // give me person id
        }
    }
}

到目前为止,我的应用程序运行正常 但我有花药我想使这项任务更简单,并花很少的时间比较 当前所用时间。我需要使用并行编程“TPL” 如何:我需要将数据库记录分成 5 个部分,每个部分大约 20,000 条记录 并以并行方式处理5个部分并等待五个部分完成 并检查是否有任何部分有这个主要这个最终结果

但我不知道我是如何植入这个想法的,希望我的问题很清楚

所以请如果有人有想法帮助我实施 这个想法我会非常感谢他

【问题讨论】:

    标签: visual-studio-2010 c#-4.0 task-parallel-library


    【解决方案1】:

    我没有测试,但希望它有所帮助。

    // define your logic here.
    Func<IEnumerable<Person>, string> YourLogicHere = null;
    // define the way to compare your task result here.
    Func<IEnumerable<Task<string>>, string> DealWithTheTaskResultsHere = null;
    Collection<Person> persons = new Collection<Person>();
    Task<string> mainTask = new Task<string>(tmpObj =>
        {
            var tmpPersons = tmpObj as Collection<Person>;
            if (tmpPersons != null)
            {
                int interval = (int)Math.Ceiling(tmpPersons.Count / 5d);
                int index = 0;
                Collection<Task<string>> subTasks = new Collection<Task<string>>();
                while (index < tmpPersons.Count)
                {
                    Task<string> subTask = new Task<string>(
                        (tmpSubPersons) => { return YourLogicHere((IEnumerable<Person>)tmpSubPersons); },
                        tmpPersons.Skip(index).Take(interval).ToArray(), TaskCreationOptions.AttachedToParent);
                    index += interval;
                    subTasks.Add(subTask);
                }
    
                foreach (var subTask in subTasks)
                {
                    subTask.Start();
                }
    
                foreach (var subTask in subTasks)
                {
                    subTask.Wait();
                }
    
                return DealWithTheTaskResultsHere(subTasks);
            }
            else return String.Empty;
        }, persons);
    
    mainTask.Start();
    mainTask.Wait();
    return mainTask.Result;
    

    【讨论】:

      【解决方案2】:

      大家好,关于我之前的问题,我找到了很好的解决方案 我将分享解决方案给每个人面对我的问题 我在网上搜索后发现 有很多方法可以对数据源进行分区。在最有效的方法中,多个线程协作处理原始源序列,而不是在物理上将源分离为多个子序列。在 .NET Framework 4 中有一个新概念可以动态分区以获取更多信息Here 和为 PLINQ 配置负载平衡分区Click Here

      我用简单的 C# 语句解释我的回答问题更清楚。

      // Get All frame from Camera you can consider it's like you are inside for loop
      for (int frame = 1; frame < 50; frame++)
      {    
        // first enhanced i process all persons in parallel way -- new enhanced
      
        Parallel.ForEach(allPersons_inFrame, perosn =>
                  {
      
                      //second enhanced Partition the entire data source
                      var rangePartitioner = Partitioner.Create(0, source.Length);
      
                       List<results> lstResult=new List<results>();
      
                      //   Loop over  the partitions in parallel. 
                      Parallel.ForEach(rangePartitioner, (range, loopState) =>
                      {
                          // Loop over each range element without a delegate invocation.
                          for (int i = range.Item1; i < range.Item2; i++)
                          {
                              // make any spacfic check to get you result
                                 lstResult.Add( source[i]);
                          }
                      });   
                  }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        • 2021-03-08
        相关资源
        最近更新 更多