Blocking

JohnE

Registered
I'm looking at the possibility of porting an app from Windows to Mac (OS9 & OSX). The Windows version uses an API function called 'GetDirectoryChangesW(...)' to receive certain notifications (specifically, it receives notifications whenever a file is added to, or changed, in a given subdirectory).

I need a similar function in the Mac version and initially tried polling (without success). Then I heard that a technique called "blocking" offers the equivalent Mac functionality. Apparently, an app can be "blocked" (i.e. suspended) until a specified event occurs.

Has anyone used this technique? Especially for receiving directory change notifications? If not, does anyone know where I can find out a bit more about blocking? Thanks in advance.
 
AppleScript has a system called Folder Actions which can be used to monitor when changes are made to a directory. It may do the trick for you.

EDIT: Turns out this isn't supported in OS9, but only in OSX. Well, it was worth a shot.
 
From what I could glean through the Apple Developer Documentation and a few Google searches, no such functionality exists. The general solution is to check each file for modification each time the application reactivates, since it is presumed that a user would be changing files around only after deactivating the app. It sounds like your app is going to be a background app, so this solution wouldn't apply. "Blocking" is a general technique for making your program wait on any external event - this includes a synchronous call to ReadFile which waits until the file has been read, rather than continuing your program and delivering a completion notification at some future time. Neither does OS X support the 'asynchronous procedure call' mechanism, which queues routines to be called when a thread goes to sleep. This is implemented instead in the user-space libraries Carbon and Cocoa, which use the main thread as an event-loop while synchronous I/O calls are run on secondary threads. The secondary threads post events to the main event-loop when they complete.
 
Thanks for the effort, anyway. Just out of interest, did you come across the following advice for handling system events using Carbon?

Carbon Event Handling

It gives 3 situations where blocking might be appropriate:- (a) watching a network socket to see when new data is available, (b) detecting when the mouse has moved, (c) detecting whether the Option key is up or down. The last example includes some code for a speech synthesizer that says "DOWN" when the Option key gets pressed and "UP" when it gets released.

I can't believe that this sort of system change can be notified to an app but changes to files and folders aren't covered by the same technique...?!?
 
Back
Top