Delphi FMX Running Parallel commands?

  

I need to crop two portions of one paged pdf and them join as simple image.

i am using below code to crop pdf using PDFToCairo.exe :

var
Output,Dim1,Dim2,PDFFile: String;
Edit1:TEdittext;

procedure Run(const ACommand, AParameters: String; CardType: String; Image:TImage);
const
CReadBuffer = 2400;
var
saSecurity: TSecurityAttributes;
hRead: THandle;
hWrite: THandle;
suiStartup: TStartupInfo;
piProcess: TProcessInformation;
pBuffer: array[0..CReadBuffer] of AnsiChar;
dBuffer: array[0..CReadBuffer] of AnsiChar; //<—– update
dRead: DWord;
dRunning: DWord;
dPresent: DWord;
begin
saSecurity.nLength := SizeOf(TSecurityAttributes);
saSecurity.bInheritHandle := True;
saSecurity.lpSecurityDescriptor := nil;
if CreatePipe(hRead, hWrite, @saSecurity, 0) then
try
FillChar(suiStartup, SizeOf(TStartupInfo), #0);
suiStartup.cb := SizeOf(TStartupInfo);
suiStartup.hStdInput := hRead;
suiStartup.hStdOutput := hWrite;
suiStartup.hStdError := hWrite;
suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
suiStartup.wShowWindow := SW_HIDE;
if CreateProcess(nil, PChar(ACommand + ‘ ‘ + AParameters), @saSecurity,
@saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess)
then
try
repeat
dRunning := WaitForSingleObject(piProcess.hProcess, 100);
PeekNamedPipe(hRead, nil,0,nil,@dPresent,nil);
if (dPresent > 0) then
repeat
dRead := 0;
ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);
pBuffer[dRead] := #0;
OemToCharA(pBuffer, dBuffer);
until (dRead < CReadBuffer);
Application.ProcessMessages();
until (dRunning <> WAIT_TIMEOUT);
Finally
CloseHandle(piProcess.hProcess);
CloseHandle(piProcess.hThread);
if CardType=’CardA’ then
begin
if FileExists(output) then
begin
Output:=GetCurrentDir+’\tools/CB-1.png’;
Run(GetCurrentDir+’\pdftocairo.exe’,’-upw ‘+ Form2.Edit1.Text +’ -cropbox -png ‘+ dimP2 +’ ‘ + PdfFile+’ tools/PCb’,’CardB’,Form2.Image1)
end else
ShowMessage(‘Password Error’);
end;

if CardType=’CardB’ then
begin
if FileExists(output) then
begin
ProcessCards(CardType); //Joining Images Here
end else
begin
ShowMessage(‘Some Error Occured’);
Form2.AniIndicator1.Visible:=False;
end;
end;
end;
Finally
CloseHandle(hRead);
CloseHandle(hWrite);
end;
end;

//On OnTTargetDrop

procedure TForm2.DropTarget1Dropped(Sender: TObject; const Data: TDragObject;
const Point: TPointF);
begin
PdfFile:= Data.Files[0];
Caption:= ‘PDFLoaded: ‘+ ExtractFileName(PdfFile);
end;

///On Button Click
Ini := TMemINIFile.Create(‘values.ini’);
dimP1:= INI.ReadString(‘Card1′,’dimeP1’,”);
dimP2:= INI.ReadString(‘Card1′,’dimeP2′,”);
Output:= GetCurrentDir+’\tools/CA-1.png’;
Run(executable,’-upw ‘+ Edit1.Text +’ -cropbox -png ‘+ dimP1 +’ ‘ + PdfFile+’ tools/CA’,’CardA’,Image1);

Using this code i can run or give one command at time to pdftocairo.exe and then on completion of first one(took 5sec) it will start second command which will crop other portion of pdfpage and will take next 5 seconds.

How can i run or give multiple commands to pdftocairo at same time so i can save the 5 seconds of second crop as it waits for the first one to complete.

and this code can’t read errors(ie: wrong password) from the command executaion i need to handle this in very dependent way as it looks for the first cropped image in the folder if it’s found then it start next command.

Is there any better approach to do this ?

Comments are closed.