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

I am writing a POE::Component::DirWatch integrated with a SystemTray::Applet. The callback in SystemTray::Applet runs the POE script. I think in order for the actual icon to get created, the callback has to return. But since my callback will not return, no icon is created. How do I get around that one?

#!perl use warnings; use strict; use Data::Dumper; use Win32::Process; use File::Slurp; use SystemTray::Applet; use POE; # auto-includes POE::Kernel and POE::Session use POE::Component::DirWatch; my $user = "mwb"; my $app = SystemTray::Applet->new( text => 'Candela Dashboard', icon => 'C:\Documents and Settings\Mike\Desktop\Candela\dashboar +d.ico', callback => &poe_dir_watcher, immediate => 1, ); sub poe_dir_watcher { POE::Session->create( inline_states => { _start => sub { $_[KERNEL]->yield("next") }, next => sub { print "tick...\n"; $_[KERNEL]->delay(next => 1); }, }, ); my $watcher = POE::Component::DirWatch->new( alias => 'dirwatch', directory => 'V:', file_callback => sub{ load_crm($_[0]) }, interval => 1, ); POE::Kernel->run(); return; } sub load_crm { my ($file) = @_; my @data; if ($file =~ /$user/) { my @data = read_file("$file"); chomp @data; # replace ^ with | for (2..4) { ($data[$_] = $data[$_]) =~ s/\^/|/g; } my $result = run_firefox(\@data); unlink($file) if $result; } } sub run_firefox { my ($data) = @_; my $firefox = 'C:\Program Files\Mozilla Firefox\firefox.exe'; my $crm_url = "firefox http://ext-test.candelacorp.com/crm/nomo.pl" +. "?cust_num=" . $data->[0] . "&cont_num=" . $data->[1] . "&prod_num=" . $data->[2] . "&prod_qty=" . $data->[3] . "&prod_prc=" . $data->[4] ; print $crm_url, "\n"; Win32::Process::Create( my $ProcessObj, $firefox, $crm_url, 0, NORMAL_PRIORITY_CLASS, "." ) || die ErrorReport(); return 1; } sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); }

Replies are listed 'Best First'.
Re: Running a SystemTray::Applet with a callback in infinite loop
by roboticus (Chancellor) on Mar 25, 2010 at 10:30 UTC

    initself:

    Your callback *must* return. If you need to make something operate continuously, then you need to fork off another process (or create a thread) and then work out the details of communicating between the processes/threads (perlipc).

    ...roboticus

Re: Running a SystemTray::Applet with a callback in infinite loop
by BrowserUk (Patriarch) on Mar 25, 2010 at 13:50 UTC

    Have you looked inside SystemTray::Applet? Maybe I am missing something, but it doesn't appear to do anything useful.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Maybe you're supposed to have one of the SystemTray::Applet plugins installed, although that doesn't seem too clear to me either.

        I thought of that, but the only one I found was SystemTray::Applet::CmdLine, which doesn't seem to do much of anything either. There is mention of subclassing, so maybe you're supposed to create you own subclass with the appropriate implementation, but then the utility of the meagre and restrictive interface is questionable.

        Seems I looked in the worng place. I should have clicked your link.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        Well I can get the app to run and display in the SystemTray, just not with a callback.

        The callback takes hold of the object creation right at ->new.