英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论