开发者

Any gottcha's on using TTimer to monitor file dates and update screen?

开发者 https://www.devze.com 2022-12-21 22:32 出处:网络
I want to detect when a file date changes and update a DevX TdxMemData which is used as a Tdatasource which then would be seen in a TDBGrid that uses it.

I want to detect when a file date changes and update a DevX TdxMemData which is used as a Tdatasource which then would be seen in a TDBGrid that uses it.

I've found开发者_JAVA百科 some code that uses ReadDirectoryChangesW, but seems rather complex for my simple needs.

I'm considering using a TTimer and firing it off every five seconds. (That's fine enough accuracy for me.)

Are there any caveats in doing this? I've read that Threads have all sorts of restrictions on VCL access, etc. Does the same thing apply to TTimer events?

Is there anything I need to watch out for when calling FileAge and updating a DevEx TdxMemData object while in a Timer event? Will those updates be seen by my main app?

Is there a way to detect the "state" of my program when a Timer event gets control so I can avoid problems?

Or am I opening an enormous can of worms in thinking about using a TTimer for this?


TTimer events are called within the main application thread, so there's no problems with accessing VCL objects from them. It's called when your application is idle, so it won't take place while your in an OnClick handler or anything similar unless you manually call Application.ProcessMessages.

I'd suggest using ReadDirectoryChangesW though. If you use a timer you will continue polling even if the application is idle and the file isn't changing. It will keep your CPU from going idle and keep could keep the hard drive from spinning down, which can have negative effects for power saving and battery usage.


In Demos directory there's "ShellChangeNotifier" component, which will fire events when files get modified, added or deleted inside directory. However it has only one OnChange event, so you don't know what really happened.

There's some discussion and solution about the issue in about.com


Windows lets you monitor file changes. As a jump start see http://delphi.about.com/od/kbwinshell/l/aa030403a.htm. There are several ready made components available, too. Google for "delphi monitor file change" or something similar


You can check my: DirectoryWatch

It is a wrapper around "ReadDirectoryChangesW" functions. It is more specific about changes than "ShellChangeNotifier".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号