Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^2: Win32::SysTray Issue ( Win32::GUI::DoEvents )

by PilotinControl (Pilgrim)
on Oct 19, 2020 at 20:11 UTC ( #11123004=note: print w/replies, xml ) Need Help??

in reply to Re: Win32::SysTray Issue ( Win32::GUI::DoEvents )
in thread Win32::SysTray Issue

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 works fine using your random number example. Thanks in advance.

Replies are listed 'Best First'.
Re^3: Win32::SysTray Issue ( Win32::GUI::DoEvents )
by jcb (Vicar) on Oct 19, 2020 at 23:46 UTC

    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; }, ); }

        This whole program probably would make more sense using Tk for a full GUI, but the Prompt sub I gave you earlier should handle the non-blocking input problem you have. Add $| = 1; just after line 9 above, paste in the sub Prompt somewhere and replace lines 23-25 with: my $input = Prompt("? ");

      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.

        I forgot to mention setting $| = 1; before the first print to turn off line-buffering on STDOUT. Oops.

        Preventing the icon feature from blocking the main program is exactly why there is a Win32::GUI::DoEvents call in the loop in the Prompt sub.

Re^3: Win32::SysTray Issue ( readline )
by Anonymous Monk on Oct 20, 2020 at 00:13 UTC

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

    Poke is a poor name for a Prompt function

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11123004]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-11-27 09:41 GMT
Find Nodes?
    Voting Booth?

    No recent polls found