# File lib/phusion_passenger/standalone/app_finder.rb, line 77
        def monitor(termination_pipe)
                raise "You must call #scan first" if !@apps
                
                watcher = PhusionPassenger::Utils::FileSystemWatcher.new(@watchlist, termination_pipe)
                if wait_on_io(termination_pipe, 3)
                        return
                end
                
                while true
                        changed = watcher.wait_for_change
                        watcher.close
                        if changed
                                old_apps = @apps
                                # The change could be caused by a write to some passenger.conf file.
                                # Wait for a short period so that the write has a chance to finish.
                                if wait_on_io(termination_pipe, 0.25)
                                        return
                                end
                                
                                new_apps = scan
                                watcher = PhusionPassenger::Utils::FileSystemWatcher.new(@watchlist, termination_pipe)
                                if old_apps != new_apps
                                        yield(new_apps)
                                end
                                
                                # Don't process change events again for a short while,
                                # but do detect changes while waiting.
                                if wait_on_io(termination_pipe, 3)
                                        return
                                end
                        else
                                return
                        end
                end
        ensure
                watcher.close if watcher
        end