如何检查PROGRAM空闲时间,而不是SYSTEM空闲时间? [英] How do I check for PROGRAM idle time, as opposed to SYSTEM idle time?

查看:93
本文介绍了如何检查PROGRAM空闲时间,而不是SYSTEM空闲时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,偶尔需要递归扫描某些目录(该程序的这一部分正在改进中,但暂时无法使用)。为了避免用户不得不等待此扫描,我希望在用户不使用我的程序时进行扫描。

I have a program that occasionally needs to scan some directories recursively (an improvement for this part of the program is in the pipeline, but won't be ready for a while). To avoid the user having to wait for this scanning, I would like to do the scanning while the user isn't using my program when possible.

我打算通过运行计时器来实现它检查空闲时间。我发现以下检查系统空闲时间的方法:
http://www.delphitips.net/2007/11/11/how-to-detect-system-idle-time/

I intend to implement it by running a timer that checks for idle time. I've found the following for checking system idle time: http://www.delphitips.net/2007/11/11/how-to-detect-system-idle-time/

这将起作用,但是即使用户正在使用同一台计算机上的其他程序,我也希望激活该功能。这样,如果他切换到另一个程序,我可以赶上我需要做的扫描。

This would be functional, but I would prefer to activate the function even when the user is working with other programs on the same computer. This way, if he switches to another program, I could catch up on the scanning I need to do.

我意识到我可以在后台线程中进行扫描,或者

I realize that I could do the scanning in a background thread, and either that or some sort of windows hooks will be implemented at some point, but not just yet.

编辑:
此处的目标是对程序进行比较简单的更改以执行任何扫描操作用户未积极使用我的应用程序时可能会排队。扫描的强度不是很高,但是它不是在线程中完成的,因此在进行过程中会冻结我的应用程序。基本上,我在寻求长期解决方案时正在寻求快速的胜利。

The goal here is a relatively easy change to the program to do any scanning that might be queued while the user isn't actively using MY application. The scanning isn't especially intensive, but it isn't done in a thread, and therefore freezes my app while in progress. Basically, I'm looking for a quick win while I'm working on a more long term solution.

推荐答案

使用< a href = http://docwiki.embarcadero.com/VCL/zh-CN/AppEvnts.TCustomApplicationEvents.OnIdle> Application.OnIdle 事件。当您的程序没有更多要处理的窗口消息时,将触发该事件。键盘和鼠标都生成消息,因此,如果没有更多消息,则即使有焦点,用户也不会使用您的程序。

Use the Application.OnIdle event. That event is triggered when your program has no more window messages to handle. The keyboard and mouse both generate messages, so if there are no more messages, then the user is not using your program, even if it has the focus.

procedure TJasonForm.ApplicationEventsIdle(Sender: TObject; var Done: Boolean);
var
  NoMoreFiles: Boolean;
begin
  // TODO: Scan for the next file
  Done := NoMoreFiles;
end;

只要 Done 为False如果队列中没有消息,则程序将继续调用该事件处理程序,从而使您可以找到更多文件。当用户产生一些输入时,您的程序将在再次调用 OnIdle 之前处理消息。

As long as Done is False and there are no messages in the queue, your program will continue to call that event handler, allowing you to find more files. When the user generates some input, your program will handle the messages before calling OnIdle again.

完成扫描文件系统后,将 Done 设置为True,以使程序停止重新调用 OnIdle 处理程序。如果您忽略了此操作,那么您的程序将重复调用一个不执行任何操作的事件处理程序,从而重复使用所有可用的CPU时间。

When you've finished scanning the file system, set Done to True so that the program stops re-calling the OnIdle handler. If you neglect to do that, then your program will use all available CPU time repeatedly calling an event handler that does nothing.

要执行此操作,您需要使用非递归搜索例程。递归搜索将在返回之前搜索整个文件系统,但是如果这样做,则 OnIdle 处理程序将挂起您的程序,这与您想要的相反。您可以使用目录名称的队列,每次触发事件时,将一个项目从队列中弹出,进行搜索,然后将其内容添加到队列的末尾。

For this to work, you'll need to use a non-recursive search routine. A recursive search will search the whole file system before returning, but if you do that, then the OnIdle handler will hang your program, which is the oposite of what you want. You can use a queue of directory names, and each time the event is fires, pop one item off the queue, search it, and add its contents to the end of the queue.

这篇关于如何检查PROGRAM空闲时间,而不是SYSTEM空闲时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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