I’m starting a project for barcode reading in a FMX/Android app (new to barcode reading, I’ve some experience with FMX).
I found this open-source ZXing.Delphi library (greatly helpful) and I went through the code of this example.
I may not be used enough to TThread but I’m wondering on the use of TThread.Synchronize in this case, because I have never seen/used it like this before.
1 – The function responsible for capturing + scanning an image in search of a barcode is GetImage. It is sync’d to main thread on CameraComponent1SampleBufferReady.
procedure TMainForm.CameraComponent1SampleBufferReady(Sender: TObject;
const ATime: TMediaTime);
begin
TThread.Synchronize(TThread.CurrentThread, GetImage);
end;
2 – GetImage function contains TTask.Run which again makes use of TThread.Synchronize to sync to the main thread.
procedure TMainForm.GetImage;
var scanBitmap: TBitmap; ReadResult: TReadResult;
begin
CameraComponent1.SampleBufferToBitmap(imgCamera.Bitmap, True);
…
scanBitmap := TBitmap.Create();
scanBitmap.Assign(imgCamera.Bitmap);
ReadResult := nil;
// There is bug in Delphi Berlin 10.1 update 2 which causes the TTask and
// the TThread.Synchronize to cause exceptions.
// See: https://quality.embarcadero.com/browse/RSP-16377
TTask.Run(
procedure
begin
try
FScanInProgress := True;
try
ReadResult := FScanManager.Scan(scanBitmap);
except
on E: Exception do
begin
TThread.Synchronize(nil,
procedure
begin
lblScanStatus.Text := E.Message;
end);
exit;
end;
end;
TThread.Synchronize(nil,
procedure
begin
…
if (ReadResult <> nil) then
begin
Memo1.Lines.Insert(0, ReadResult.Text);
end;
end);
finally
ReadResult.Free;
scanBitmap.Free;
FScanInProgress := false;
end;
end);
end;
? : Is it usual/good pratice to encapsulate TThread.Synchronize( TTask.Run( TThread.Synchronize(…) ) ); ?
? : Couldn’t it be the cause of the mentionned exceptions encountered in Delphi 10.1 update 2 ?
When I “learned” to use Parallel Programming Library, I mainly used :
this entry on Emb’ro
Malcolm Groves’ blog
this CodeRage 9 video
Did I missed it somewhere ?