here is the actual code of scan and scanThread is the TMyThread (TThread) and i called scanThread.Resume at the bottom
procedure TfrmMain.btnEnqueueClick(Sender: TObject);
var
scanThread : TMyThread;
begin
scanThread := TMyThread.Create(True);
scanThread.venString := '';
scanThread.x264String := '';
if (cboMode.Text = 'CRF') and (txtVideoValue.Text <> '') then
begin
scanThread.venString := '-q ';
end
else if (cboMode.Text = 'Bitrate') and (txtVideoValue.Text <> '') then
begin
scanThread.venString := '-2 -b ';
end
else if (cboMode.text = 'File Size') and (txtVideoValue.Text <> '') then
begin
scanThread.venString := '-2 -S ';
end;
try
if (strToInt(txtVideoValue.Text) < 10) and (cboMode.Text = 'CRF') then
begin
showmessage('CRF value must be >= 20 and <= 33');
txtVideoValue.Text := '';
txtVideoValue.SetFocus;
end;
except
Exit;
end;
try
if (strToInt(txtAudioValue.Text) < 10) then
begin
showmessage('Audio bitrate must be not less than 48');
txtAudioValue.text := '';
txtAudioValue.SetFocus;
end;
except
Exit;
end;
if rbHigh.Checked = true then
begin
scanThread.x264String := 'deblock=1,1:ref=8:bframes=5:me=umh:subme=10:trellis=2:psy-rd=0.4:aq-strength=0.6:aq-mode=2';
end
else if rbMedium.Checked = true then
begin
scanThread.x264String := 'deblock=1,1:ref=5:bframes=3:me=umh:subme=8:trellis=1:psy-rd=0.4:aq-strength=0.6';
end
else if rbLow.Checked = true then
begin
scanThread.x264String := 'deblock=1,1:ref=3:bframes=3:trellis=1:psy-rd=0.4:aq-strength=0.6';
end;
if lstAdd.items.count = 0 then exit;
if (txtVideoValue.text <> '') and (txtAudioValue.Text <> '') and (txtWidth.Text <> '') and (txtHeight.text <> '') then
begin
scanThread.sPro := TProcess.Create(nil);
scanThread.M := TMemoryStream.Create;
scanThread.BytesRead := 0;
scanThread.audioCount := 0;
i := 0;
scanThread.scanned := false;
//tmrScan.Enabled := true;
btnEncode.Enabled := false;
scanThread.Resume;
end;
end;
here is the Thread class now
TMyThread = class(TThread)
private
procedure Scan;
procedure iUpdate;
procedure prgUpdate;
protected
procedure Execute; override;
public
scanned: boolean;
audioCount: integer;
venString: string;
x264String: string;
sPro: TProcess;
M: TmemoryStream;
n: longInt;
BytesRead: longInt;
end;
and i put synchronize action on the thread execute now
implementation
procedure TMyThread.Execute;
begin
Scan;
end;
procedure TMyThread.iUpdate;
begin
frmMain.i := frmMain.i + 1;
end;
procedure TMyThread.prgUpdate;
begin
frmMain.prgScan.StepIt;
if frmMain.prgScan.Position = frmMain.prgScan.Max
then frmMain.prgScan.Position := frmMain.prgScan.Min;
end;
procedure TMyThread.Scan;
const
READ_BYTES = 2048;
var
x : integer;
audioTracks: string;
begin
audioTracks := '';
if (scanned = false) and (frmMain.i < frmMain.lstAdd.Items.Count) then
begin
sPro.CommandLine := 'HandbrakeCLI -i ' + '"' + frmMain.lstAdd.Items[frmMain.i] + '"' + ' -t 0';
sPro.Options := [poUsePipes, poStderrToOutPut];
sPro.ShowWindow := swoShow;
sPro.Execute;
while sPro.running do
begin
M.SetSize(BytesRead + READ_BYTES);
n := sPro.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
if n > 0 then
begin
Inc(BytesRead,n);
Synchronize(@prgUpdate);
end;
end;
repeat
Synchronize(@prgUpdate);
M.SetSize(BytesRead + READ_BYTES);
n := sPro.Output.Read((M.Memory + BytesRead)^, READ_BYTES);
if n > 0 then
begin
Inc(BytesRead,n);
end;
until n <= 0;
M.SetSize(BytesRead);
frmMain.HbkStr.LoadFromStream(M);
if frmMain.HbkStr.IndexOf('No title found.') <> -1 then
begin
scanned := true;
exit;
end;
audioCount := 0;
x := frmMain.HbkStr.IndexOf(' + audio tracks:') + 1;
repeat
x := x + 1;
AudioCount := AudioCount + 1;
Synchronize(@prgUpdate);
until (x = frmMain.HbkStr.Count - 1) or (x = frmMain.HbkStr.IndexOf(' + subtitle tracks:')) ;
for x := 1 to AudioCount do begin
Synchronize(@prgUpdate);
audioTracks := audioTracks + intToStr(x);
if x <> AudioCount then
audioTracks := audioTracks + ',';
end;
if frmMain.chkHardSubs.Checked = true then
begin
frmMain.lstOut.Items.add('HandbrakeCLI -i ' + '"' + trim(frmMain.lstAdd.Items[frmMain.i]) + '" ' + ' -f mkv -o ' + '"' + trim(frmMain.lstAdd.Items[frmMain.i]) + '-OUT.mkv' + '" ' + venString + trim(frmMain.txtVideoValue.Text) + ' -e x264 -x ' + x264String + ' -E vorbis -B '+ trim(frmMain.txtAudioValue.Text) + ' -w ' + trim(frmMain.txtWidth.Text) + ' -l ' + trim(frmMain.txtHeight.Text) + ' -s 1 --subtitle-burn 1' + ' -a ' + audioTracks);
end
else if frmMain.chkHardSubs.Checked = false then
begin
frmMain.lstOut.Items.add('HandbrakeCLI -i ' + '"' + trim(frmMain.lstAdd.Items[frmMain.i]) + '" ' + ' -f mkv -o ' + '"' + trim(frmMain.lstAdd.Items[frmMain.i]) + '-OUT.mkv' + '" ' + venString + trim(frmMain.txtVideoValue.Text) + ' -e x264 -x ' + x264String + ' -E vorbis -B '+ trim(frmMain.txtAudioValue.Text) + ' -w ' + trim(frmMain.txtWidth.Text) + ' -l ' + trim(frmMain.txtHeight.Text) + ' -a ' + audioTracks);
end;
Synchronize(@prgUpdate);
scanned := true;
end;
if (scanned = true) and (frmMain.i < frmMain.lstAdd.Items.Count) and (sPro.Running = false) then
begin
//i := i + 1;
Synchronize(@iUpdate);
scanned := false;
Synchronize(@prgUpdate);
end;
if (scanned = false) and (frmMain.i = frmMain.lstAdd.Items.Count)then
begin
//frmMain.tmrScan.Enabled := false;
Synchronize(@prgUpdate);
ShowMessage('Scanning Done');
frmMain.btnEncode.Enabled := true;
scanned := true;
frmMain.prgScan.Position := frmMain.prgScan.Min;
frmMain.lstAdd.Clear;
frmMain.tbsOutput.Show;
//sPro.Free;
//M.Free;
end;
end;
still no luck im getting that raised exception class 'EThread' with message: CheckSynchronize called from non-main thread