How do I check for PROGRAM idle time, as opposed to SYSTEM idle time?

前端 未结 3 2239
慢半拍i
慢半拍i 2021-02-20 02:11

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).

相关标签:
3条回答
  • 2021-02-20 02:33

    What you need is a separate thread that will be resumed when the system is idle or paused when there is activity. A simple way to do this is to place a Timer Component and check if the system is idle and thread is suspended or not use ResumeThread ,SuspendThread

    Look here

    http://pastebin.com/8X9Sg42H i crafted something for your situation enjoy

    0 讨论(0)
  • 2021-02-20 02:42

    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;
    

    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.

    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.

    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.

    0 讨论(0)
  • 2021-02-20 02:45

    ...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.

    Stop the scanning in Application.OnActivate and resume in Application.OnDeactivate.

    0 讨论(0)
提交回复
热议问题