Delphi回调中的GUI问题

huangapple go评论83阅读模式
英文:

Delphi issues with GUI in a Callback

问题

我有一个工作非常好的C++视频处理DLL,但它的回调函数会冻结我的应用程序GUI;以下是我的Delphi代码:

procedure FramesDone_cb(pvfDone: Integer; var cancel: Boolean); cdecl;
begin
    // 这个回调函数会冻结整个GUI
    Form1.ProgressBar1.Position := pvfDone;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    vp: VideoProcessor;
begin
    vp := VideoProcessor_Create();
    VideoProcessor_SetFramesDone_cb(vp, FramesDone_cb);
end;

我尝试在回调函数中使用匿名线程,但总是出现“线程创建错误”,请问是否有修复/更合适的方法,以避免这个回调函数冻结我的应用程序GUI。

英文:

I've a C++ video processing DLL working very well but its callback is freezing my APP GUI ; here's my Delphi code :

procedure FramesDone_cb(pvfDone: Integer;var cancel:Boolean);cdecl;
begin
    // this callback is freezing the whole GUI
    Form1.ProgressBar1.Position := pvfDone;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
vp: VideoProcessor;
 begin
  vp := VideoProcessor_Create();
  VideoProcessor_SetFramesDone_cb(vp,FramesDone_cb);
end;

I tried to put an anonymous thread in the callback but i always get Thread creation error , please is there any fix/proper way so that this CB doesn't freeze my App GUI .

答案1

得分: 1

我建议在您的回调函数中更新原子变量,然后在定时器事件中更新进度条,触发频率不要超过每次显示刷新一次。

procedure FramesDone_cb(pvfDone: Integer; var cancel: Boolean); cdecl;
begin
    // CurrentProgress 是一个整数,因此可以原子方式更新
    CurrentProgress := pvfDone;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
    if ProgressBar1.Position <> CurrentProgress then
        ProgressBar1.Position := CurrentProgress;
end;

这将解耦回调函数与用户界面,并避免过多地更新进度条。

英文:

I would suggest updating an atomic variable in your callback then update the progress bar in a timer event that triggers not more often than once per display refresh.

procedure FramesDone_cb(pvfDone: Integer;var cancel:Boolean);cdecl;
begin
   // CurrentProgress is an integer so can be updated atomically
   CurrentProgress := pvfDone;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if ProgressBar1.Position != CurrentProgress then
     ProgressBar1.Position := CurrentProgress;
end;

This decouples the callback from the UI and avoids excessive updates of the progress bar.

huangapple
  • 本文由 发表于 2020年1月6日 16:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定