Delphi空闲处理程序只在我移动鼠标时触发 [英] Delphi idle handler only fires when I move the mouse

查看:187
本文介绍了Delphi空闲处理程序只在我移动鼠标时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的D2006应用程序中有一个OnIdle处理程序。使用以下代码:

 程序TMainForm.ApplicationEvents1Idle(发件人:TObject; var Done:Boolean); 

begin
Inc(IdleCalls);
睡眠(10);
完成:= False;
结束

该应用运行顺利,空闲处理程序每​​秒调用100次,CPU使用量为下一个为零。



然后,我添加了一个TActionList并将一些控件连接到操作上,编码了一个Execute和Update处理程序。

 程序TMainForm.ActionNewButtonExecute(Sender:TObject); 
begin
DoNewProject;
结束

procedure TMainForm.ActionNewButtonUpdate(Sender:TObject);
begin
ActionNewButton.Enabled:= AccessLevelIsSupervisor;
结束

问题。 OnUpdate事件不会触发。在一个预感中,我在OnIdle处理程序中设置了Done:= true,而OnIdle处理程序在移动鼠标时才被调用。而更新操作仍然不会触发。



为什么更新处理程序不能触发,应该将Done设置为true还是false?还是两者?

解决方案

如评论中所述, Sleep 空闲的处理程序将不会很好,如果应用程序没有任何活动,也会停止处理。



然而,您可以降低CPU使用情况,而且会产生很多令人不安的影响:
处理所有 OnIdle 事件,应用程序将调用 WaitMessage (消息队列为空时将休眠),如果完成参数是 True - 您可以无条件地将其设置在处理程序中。



对于后台处理,请使用线程和通过同步回到主线程,或者如果你真的需要,使用定时器,不要忘记处理重入(两种解决方案都可以在 WaitMessage )之间唤醒应用程序。


I have an OnIdle handler in my D2006 app. With this code:

procedure TMainForm.ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);

begin
Inc (IdleCalls) ;
Sleep (10) ;
Done := False ;
end ;

the app runs smoothly, the idle handler is called 100 times per second, and the CPU usage is next to zero.

I then added a TActionList and connected up some controls to actions, coded an Execute and Update handler.

procedure TMainForm.ActionNewButtonExecute(Sender: TObject);
begin
DoNewProject ;
end ;

procedure TMainForm.ActionNewButtonUpdate(Sender: TObject);
begin
ActionNewButton.Enabled := AccessLevelIsSupervisor ;
end;

Problem. The OnUpdate event doesn't fire. On a hunch I set Done := true in the OnIdle handler and the OnIdle handler is then only called when I move the mouse. And the Update action still doesn't fire.

Why might the Update handler not be firing, and should I set Done to true or false? Or both?

解决方案

As mentioned in the comments, Sleep in the idle handler will do no good, also the bacground processing will stall if there is no activity on the application.

You can however lower the CPU usage w/o much disturbing effects:
After processing all OnIdle events, the application will call WaitMessage (which will sleep while the message queue is empty), if the Done parameter is True - you can just unconditionally set it in your handler.

As for background processing, use either a thread and call back to the main thread via Synchronize or, if you really-really have to, use a timer and don't ever forget to handle reentrancy (both solutions will by the way wake the application even while WaitMessage).

这篇关于Delphi空闲处理程序只在我移动鼠标时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆