How to multi-thread my code to process multiple files at the same time with the same function?

I use the routine below to process a group of videos from a directory:

AllVideos = new List<MyVideo>();

for (int i = 0; i < AllVideosFileNames.Length; i++)
{
    MyVideo CurrentVid = new MyVideo(AllVideosFileNames[i], false); // constructor (loads the video to the system) 
    CurrentVid.PopulateAllFrames(); // Method that takes forever
    CurrentVid.PopluateTestFrames(); // Method that takes less than forever
    CurrentVid.NormalizeTestFrames(); // Method that takes some time
    AllVideos.Add(CurrentVid); // Add finished object to my results container
}

I would like to process several videos at the same time, in parallel and save time. I tried to do this:

for (int i = 0; i < AllVideosFileNames.Length; i++)
{
    Thread TempThread = new Thread(() => 
    {
        MyVideo CurrentVid = new MyVideo(AllVideosFileNames[i], false);
        CurrentVid.PopulateAllFrames();
        CurrentVid.PopluateTestFrames();
        CurrentVid.NormalizeTestFrames();
        AllVideos.Add(CurrentVid);
    });
    TempThread.Start();
}

However, this is so fast and literally does nothing. I do not mean anything when passing through the code, it is AllVideosempty when the loop completes execution.

Any ideas for a better design? or fix? Please thanks.

+4
source share
2 answers

Perhaps look in TPL and / or PLINQ. Do you know about .AsParallel()?

What about:

AllVideos = AllVideosFileNames
    .AsParallel()
    .Select(fileName => GetVideo(filename))
    .ToList();

Where is GetVideo(filename)defined as follows:

private MyVideo GetVideo(string filename)
{
    MyVideo currentVid = new MyVideo(filename, false);
    currentVid.PopulateAllFrames();
    currentVid.PopluateTestFrames();
    currentVid.NormalizeTestFrames();
    return currentVid;
}

. PLINQ.

+3

Parallel.ForEach. , , :

Parallel.ForEach(AllVideosFileNames, item =>
{
    MyVideo CurrentVid = new MyVideo(item, false);
    CurrentVid.PopulateAllFrames();
    CurrentVid.PopluateTestFrames();
    CurrentVid.NormalizeTestFrames();
    AllVideos.Add(CurrentVid);

});

:

List AllVideos . :

Parallel.ForEach(AllVideosFileNames, item =>
{
    MyVideo CurrentVid = new MyVideo(item, false);
    CurrentVid.PopulateAllFrames();
    CurrentVid.PopluateTestFrames();
    CurrentVid.NormalizeTestFrames();
    lock (AllVideos)
    {
        AllVideos.Add(CurrentVid);
    }    
});
+3

Source: https://habr.com/ru/post/1672918/


All Articles