简体   繁体   English

ZXing.Delphi-正确使用TThread.Synchronize吗?

[英]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.

相关问题 TThread.Synchronize在Delphi 2009中导致(接近)死锁(在Delphi 7中工作) - TThread.Synchronize causing (near) deadlock in Delphi 2009 (worked in Delphi 7) 是TThread.Synchronize邪恶/死产? - Is TThread.Synchronize evil / stillborn? 在主线程中调用 TThread.Synchronize - Calling TThread.Synchronize in main thread 我可以将Delphi TThread.Synchronize()本地移动到VCL表单以从主线程或工作线程调用吗? - Can I move Delphi TThread.Synchronize() locally to a VCL form to be called from both a main or worker thread? 使用Delphi TThread - Working with Delphi TThread 在主线程和子线程之间使用TThread的“同步”或使用窗口消息进行IPC更好吗? - Is it better to use TThread's “Synchronize” or use Window Messages for IPC between main and child thread? Delphi 中检查 FreeOnTerminate = True 的非暂停创建的 TThread 是否仍在执行的正确方法是什么? - What's the correct way in Delphi to check if a non-Suspended created TThread with FreeOnTerminate = True is still executing? 多线程(TThread)Delphi 应用程序不会终止 - Multithreaded (TThread) Delphi application will not terminate Delphi 检查 TThread 是否仍在运行 - Delphi check if TThread is still running TThread :: Synchronize()&gt;读取表单组件属性时? - TThread::Synchronize() > when reading Form component properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM