[英]ZXing.Delphi - Correct use of TThread.Synchronize?
I'm starting a project for barcode reading in a FMX/Android app (new to barcode reading, I've some experience with FMX). 我正在启动一个在FMX / Android应用中读取条形码的项目(条形码读取的新手,我对FMX有一定的经验)。
I found this open-source ZXing.Delphi library (greatly helpful) and I went through the code of this example . 我找到了这个开源的ZXing.Delphi库 (非常有帮助),并仔细阅读了本示例的代码。
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. 我可能对
TThread
不够用,但是我想知道在这种情况下使用TThread.Synchronize
,因为我以前从未见过/使用过这种方法。
1 - The function responsible for capturing + scanning an image in search of a barcode is GetImage
. 1-负责捕获+扫描图像以搜索条形码的功能是
GetImage
。 It is sync'd to main thread on CameraComponent1SampleBufferReady
. 它已同步到
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. 2 -
GetImage
函数包含TTask.Run
再次利用的TThread.Synchronize
同步到主线程。
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(...) ) );
:封装TThread.Synchronize(TTask.Run(TThread.Synchronize(...)))是平时/好的做法吗? ?
?
? ? : Couldn't it be the cause of the mentionned exceptions encountered in Delphi 10.1 update 2 ?
:不是在Delphi 10.1 Update 2中遇到提到的异常的原因吗?
When I "learned" to use Parallel Programming Library, I mainly used : 当我“学习”使用并行编程库时,我主要使用:
Did I missed it somewhere ? 我在某处错过了吗?
A quick update. 快速更新。 With Embarcadero 10.3.1:
使用Embarcadero 10.3.1:
TThread.Synchronize(TThread.CurrentThread, GetImage);
generates after a random number of seconds a Segmentation Fault. 在随机数秒后生成分段错误。 You can notice a many "Switch To Thread" calls suring debug and it will always crash the APP.
您会注意到调试过程中发生了许多“切换到线程”调用,它将始终使APP崩溃。 No matter if you leave GetImage function empty.
无论是否将GetImage函数保留为空。 I've tried all the possibile ways but my app and also the original app crashes.
我尝试了所有可能的方式,但我的应用程序以及原始应用程序崩溃了。 I've also tried creating an Asyincronous thread as well as Labda... same results all the times It looks like TThread Synchronization has issues on 10.3.1.
我也曾尝试创建一个Asyincronous线程以及Labda……一直都得到相同的结果看起来TThread同步在10.3.1上有问题。 Other users reported to have it working fine on previous Tokyo version.
其他用户报告说它可以在以前的东京版本上正常工作。
Any feedback from your side? 您有任何反馈吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.