-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;
Jernej K
  • 1,602
  • 2
  • 25
  • 38
Zenith
  • 27
  • 5
  • 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. – Ken White Apr 20 '16 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 – Zenith Apr 20 '16 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. – Ken White Apr 20 '16 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 – Zenith Apr 20 '16 at 17:23
  • The OS will schedule the cores as needed, much better than you possibly could by launching multiple threads. You didn't *batch multi-thread*, because you can't multi-thread in a batch file. I'd suspect you're confused by terminology here. – Ken White Apr 20 '16 at 17:25
  • Ooopsy...I should have said "Multiple instances" – Zenith Apr 20 '16 at 17:31
  • 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*. :-) – Ken White Apr 20 '16 at 17:32

1 Answers1

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...

RoutineOp
  • 400
  • 1
  • 8
  • 1
    Agreed, which I said to the OP in my comments. And which (to point out to the OP) is not *multi-threading*. – Ken White Apr 20 '16 at 17:30
  • Can you provide me with its code please Sir...As I'm totally new to Multithreaded programming... – Zenith Apr 20 '16 at 17:34
  • Heres a link to a question with answers to that http://stackoverflow.com/questions/18013251/delphi-createprocess-execute-multiple-commands – RoutineOp Apr 20 '16 at 17:38
  • @Zenith: This answer is not describing multithreading either (as I keep trying to tell you). You need to learn terminology. – Ken White Apr 20 '16 at 17:47