jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I have a Win32/Perl/Tk application. The application needs to monitor a directory and when a file changes I need to read the file, parse out some info and then use this info within the running programme.

I assume I can use Win32::ChangeNotify for this. But how do I do this in a non-blocking way? The application will be running, other operations may be being performed when this event occurs.

Any advice? Suggestions?

jdtoronto

Replies are listed 'Best First'.
Re: Using Win32::ChangeNotify
by zentara (Cardinal) on Oct 27, 2005 at 12:50 UTC
Re: Using Win32::ChangeNotify
by EvanCarroll (Chaplain) on Oct 27, 2005 at 05:49 UTC
    1. You can thread and have and have a semiphore set when the directory is changed.
    2. You can fork, and check status.


    See perldoc perlicp, perlfork, and perlthrtut.


    Evan Carroll
    www.EvanCarroll.com
Re: Using Win32::ChangeNotify
by BrowserUk (Patriarch) on Oct 27, 2005 at 15:39 UTC

    There is no need for threading or forking. The wait() method takes a timeout in milliseconds.

    So, set up a Tk->after callback that goes off every say 1/10 of a second and call

    my $notifyObj = Win32::ChangeNotify( ... ); my $found = 0; my $abandoned = 0; sub checkNotify( my $rv = $notifyObj->wait( 0 ); $found = 1 if $rv = 1; $abandoned = 1 if $fv = -1; die 'Notify error' unless defined $rv; } $mainWindow->after( 100, \&checkNotify );

    See Win32::IPC and Win32::Semaphore for details.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.