-3

I was working on a program lately. The program extracts all the frames from a video and then processes them all...Nut, in the processing part, it's way too slow, so I thought to run those processing functions parallel...

But, as I'm new in Delphi and ain't got any experience in Multithreading, I was hoping someone would know how to do it.

Here is the function that I'm using currently:

sl.loadfromfile(log);
for i := 0 to (SL.Count div 2) - 1 do
        begin
          WriteLn('Processing extracted frames ' + IntToStr(i + 1) +
            ' of ' + IntToStr(SL2.Count div 2));
          if FileExists(TempDir + IntToHex(i, 8) + '.jpg') then
            ExecAndWait(SrcDir + 'packjpg.exe', AnsiRightStr(Str[2], 2) + ' ' +
              '"' + TempDir + IntToHex(i, 8) + '.jpg' + '" "' + TempDir +
              IntToHex(i, 8) + '.pjg' + '" , TempDir, true, true);
        end;
7
  • The code you've posted makes zero effort to do anything in threads, and as all of the work is being done by an external application it's highly unlikely that threads will help. Just stop waiting for the external app and let several instances of it run. Commented Apr 20, 2016 at 16:54
  • Well passing those instances to different cores can improve the speed way too nicely...Like it takes 10 min to process 80K files...But if it runs on 8 cores...I estimate it can do it in 1-2 min Commented Apr 20, 2016 at 16:57
  • It's not going to help anything, because the delay is in your waiting for the external app. Spawn 8 instances of the external app at a time instead. There is zero benefit to multi-threading anything in the code you've posted, and if you think otherwise you're going to be sorely disappointed. Commented Apr 20, 2016 at 16:59
  • Well Sir, that external program uses only one core...Also, there is a dll for it too, which I intend to use once I succeed in multithreading it...Also, I tested the same using a batch with multithread function & its processing time was1 min 3 sec...Which clearly shows that I won't be disappointed :D Commented Apr 20, 2016 at 17:23
  • 1
    Which is exactly what I said in my first and second comments here (launching multiple instances by not using ExecAndWAIT), which is not multithreading. :-) Commented Apr 20, 2016 at 17:32

1 Answer 1

1

What I suggest is
1. Use createProcess to execute packjpg.exe, This will execute immediately and you won't be waiting around for it to finish.
2. createProcess returns a TProcessInformation which you will need to store in a list.
3. Do this for each file you want to process.
4. you now have the files being process concurrently and a list of TProcessInformation.
5. Loop through the list of TProcessInformation you kept until all of the process are finished, using: WaitForSingleObject(aProcessInformationRecord.hProcess, INFINITE);

You can tweak things like max number of current processes etc...

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

4 Comments

Agreed, which I said to the OP in my comments. And which (to point out to the OP) is not multi-threading.
Can you provide me with its code please Sir...As I'm totally new to Multithreaded programming...
Heres a link to a question with answers to that stackoverflow.com/questions/18013251/…
@Zenith: This answer is not describing multithreading either (as I keep trying to tell you). You need to learn terminology.

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.