Run Multiple Commands in Parallel?

  

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

This is Unit2 which executes the commands:

unit Unit2;

interface

uses
System.SysUtils,FMX.Forms,WinAPI.Windows,Unit1;

procedure Run(const ACommand, AParameters: String);
procedure BadInput;
procedure ProceBits;
implementation

procedure ProceBits;
begin
Unit1.ProcessCards;
end;

procedure BadInput;
begin
raise Exception.Create(‘Wrong Password’);
end;

procedure Run(const ACommand, AParameters: String);
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;
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);
end;
Finally
CloseHandle(hRead);
CloseHandle(hWrite);
if (CardType =’Students’) or (CardType =’Workers’) or (CardType =’Family’) and (FileExists(Output))
then ProceBits else if (CardType = ”)// for the first part of the card
then {*do nothing*} else badinput;
end;
end;
end.

and below is how i call Run command:

uses Unit2;

function ValidateCardType(Const s: string) :string;
begin
if TRegex.IsMatch(s,’^[A-Z]{4}[0-9]{4}$’) then
begin
Result:= ‘Student’;
end else
if TRegex.IsMatch(s,’^[0-9{8}$]’) then
begin
Result:= ‘Workers’;
end else
if (s=’BN’) or (s=’BS’) then
begin
Result:= ‘Family’;
end else
begin
Result:= ‘Error’;
Exit
end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
var
Password,DimA,DimB: string;
vINI: TMEMINIFile;
begin
if PDFFile.IsEmpty then
begin
ShowMessage(‘Can’t Work On Empty Stomach !’);
AniIndicator1.Visible:=False;
end else
AniIndicator1.Visible:=True;
begin
vINI := TMemINIFile.Create(‘values.ini’);
Password:= Edit1.Text;
CardType:= ValidateCardType(Password);
ClearImage;

if CardType =’Student’ then
begin
DimA := vINI.ReadString(‘Card1′,’dimes’,”);
Output:= GetCurrentDir+’\tools/AC-1.png’;
Run(Executable,’-opw ‘+ Password +’ -cropbox -png ‘ + DimA + ‘ ‘ + PdfFile+’ tools/AC’);
end else

if CardType =’Workers’ then
begin
DimA := vINI.ReadString(‘Card2′,’dimesA’,”);
DimB := vINI.ReadString(‘Card2′,’dimesB’,”);
Output:= GetCurrentDir+’\tools/PCb-1.png’;
CardType:=”;
Run(Executable,’-upw ‘+ Password +’ -cropbox -png ‘ + DimA + ‘ ‘ + PdfFile+’ tools/PCa’);
CardType:=Workers;
Run(Executable,’-upw ‘+ Password +’ -cropbox -png ‘ + DimB + ‘ ‘ + PdfFile+’ tools/PCb’);
end else

if CardType =’Family’ then
begin
DimA := vINI.ReadString(‘Card3′,’dimesA’,”);
DimB := vINI.ReadString(‘Card3′,’dimesB’,”);
Output:= GetCurrentDir+’\tools/Bb-1.png’;
CardType:=”;
Run(Executable,’ -cropbox -png ‘ + DimA + ‘ ‘ + PdfFile+’ tools/Ba’);
CardType:=’Family’;
Run(Executable,’ -cropbox -png ‘ + DimB + ‘ ‘ + PdfFile+’ tools/Bb’);
end else

begin
AniIndicator1.Visible:=False;
BadInput;
end;
VINI.Free;
end;
end;

For CardType Family or Worker i need to crop two parts of the pdf.

The code works fine but the commands are running like in FIFO method, but i need to run both commands in parallel mode so i can save time that is being taken by second command till it finishes.

How can i run both commands (for CardType Family or Worker) in parallel, and fetch errors from pdftocairo.exe (such as wrong password) ?

Comments are closed.