开发者

TPL Task return values

i have application for face recognition system i received From Camera Frames around 30 : 50 Frame/Second this depending on camera type , for each Frame i have anther function to get all persons on it and from each persons in all person in current Frame I check if this person is exist in database or not Database records it’s around 100,000 records , this all steps i made for ever frame

i rewrite my previous question with simple C# statement To be more clear

// 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
        }
    }
}

till now my application is working without any problems but I have anther I dea to make this task more simple and take small time comparing with current time taken .i need to used Parallel Programming "TPL" how : i need to divide database record to 5 part each part around 20,000 record and process 5 parts in Parallel way and wait till five part finished and check i开发者_运维知识库f any part have result this main this final result

but I don’t know how I implant this idea I hope my question is clear

So please if any one has an idea to help me implement this idea I will be very grateful to him


I didn't test, but hope it helps.

// 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;


Hi everyone regarding my previous question I found good solution I will share solution for everyone face seam my problem After I searching on internet I found that There are many ways to partition a data source. In the most efficient approaches, multiple threads cooperate to process the original source sequence, rather than physically separating the source into multiple subsequences. And in .NET Framework 4 there is new concept could Dynamic Partitions for more information Here and to Configuring Load Balancing Partitioners for PLINQ Click Here

i explain my answer question with simple C# statement To be more clear .

// 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]);
                    }
                });   
            }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜