in reply to Win32::SysTray Issue

https://metacpan.org/pod/Win32::GUI::Reference::Methods#DoEvents
#!/usr/bin/perl -- use strict; use warnings; use Win32::SysTray; Main( @ARGV ); exit( 0 ); sub Main { my $tray = new Win32::SysTray ( 'icon' => shift , 'single' => 1, ) or exit 0; $tray->setMenu ( "> &Test" => sub { print "Hello from the Tray\n"; }, ">-" => 0, "> E&xit" => sub { return -1 }, ); MainLoop( $tray ); } sub MainLoop { my( $tray ) = @_; for my $i (1 .. rand 1000){ print "my $i\n"; # $tray->runApplication; # last if -1 == Poke( $tray ); die "user quit" if -1 == Poke( $tray ); Win32::Sleep( rand 1000 ); } } #~ sub Win32::SysTray::Poke { sub Poke { my( $tray ) = @_; Win32::GUI::DoEvents(); }

Replies are listed 'Best First'.
Re^2: Win32::SysTray Issue ( Win32::GUI::DoEvents )
by PilotinControl (Pilgrim) on Oct 19, 2020 at 20:11 UTC

    Hello Anon Monk
    I modified your code:

    #!/usr/bin/perl -- use strict; use warnings; use Win32::SysTray; sTray( @ARGV ); exit( 0 ); sub sTray { my $tray = new Win32::SysTray ( 'icon' => 'C:\images\logo.ico' , 'single' => 1, ) or exit 0; $tray->setMenu ( "> &Test" => sub { print "Hello from the Tray\n"; }, ">-" => 0, "> E&xit"=> sub { exit; }, ); begin( $tray ); } sub begin { my( $tray ) = @_; die "user quit" if -1 == Poke( $tray ); } #~ sub Win32::SysTray::Poke { sub Poke { my( $tray ) = @_; print "\nWhat is your name? "; chomp(my $name=<STDIN>); print "\nHello $name\n\n"; Win32::GUI::DoEvents(); }
    This is a smaller version from the larger program...a series of user inputs etc. When I run the above code it waits for an input....while I'm waiting for the input I would like to exit the program by clicking on the icon. That's what I'm trying to do....it works fine using your random number example. Thanks in advance.

      Oh! That should be easy, although this code is untested:

      # ... our $Tray = new Win32::SysTray ( ... ); # note that $Tray is global # ... my $name = Prompt("What is your name?"); print "\nHello $name\n\n"; # ... sub Prompt { my $prompt = shift; print "\n$prompt "; my $response = ''; my $rsel = ''; my $rrdy; vec($rsel,fileno(STDIN),1) = 1; CHAR: while (1) { die "user quit" if -1 == Win32::GUI::DoEvents(); while (select($rrdy=$rsel, undef, undef, 0.10)) { sysread(STDIN, $response, 1, length $response) == 1 or die "read error"; } last CHAR if chomp($response); } return $response; }

        Here is my dumbed down code as you can see I have a user input style menu and I would like to stop the program using the Icon in the system tray if possible. At the moment the program is blocked while waiting for user input. Is there a way to bypass that? Thanks again!

        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Term::ANSIScreen; use Win32::Console; use Win32::SysTray; no warnings 'numeric'; tray(); begin(); sub begin { # START BEGIN print "-------------------------------------------\n"; print "Please Choose One of the Following Options:\n"; print "-------------------------------------------\n\n\n"; print "(1) EMPLOYEE MANAGEMENT \n\n"; print "(Q) QUIT \n\n"; print " \n\n\n\n"; my $input = <STDIN>; $input = <STDIN> until defined $input; chop ($input); if ($input){ if ( $input == 1 ){ sleep(3); begin(); } if ( $input eq "q" ) { sleep 3; exit; } } # END INPUT } # END BEGIN sub tray { my $stray = new Win32::SysTray ( 'name' => 'TEST', 'icon' => 'C:\images\logo.ico', 'single' => 1, ) or exit 0; $stray->setMenu ( "> &Test" => sub { print "Hello from the Tray\n"; }, ">-" => 0, "> E&xit" => sub { exit; }, ); }

        Half the code works. It creates the Icon just fine...does NOT create the sub menu. It does NOT create the prompt message until I quit the program using Ctrl+C. A good guess would be that the Icon feature needs to break away from the main program and not block the rest of the code from working and when selected from the Icon quit the program some how? Thanks for the help so far.

      Well, what do you expect a blocking call like readline to do other than block?

      Poke is a poor name for a Prompt function