Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by jcb (Parson)
on Oct 19, 2020 at 23:46 UTC ( [id://11123008]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Win32::SysTray Issue ( Win32::GUI::DoEvents )
by PilotinControl (Pilgrim) on Oct 21, 2020 at 01:14 UTC

    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("? ");

        If these are the correct changes to be made from the code you provided....it still does not work. In fact none of the input choices work now.

        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use Term::ANSIScreen; use Win32::Console; use Win32::SysTray; no warnings 'numeric'; $| = 1; # ADDED 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 = Prompt("? "); # ADDED 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; }, ); } 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; }

Re^4: Win32::SysTray Issue ( Win32::GUI::DoEvents )
by PilotinControl (Pilgrim) on Oct 20, 2020 at 02:56 UTC

    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.

        The changes I made based on your suggestions did not work...I do have the changes in the correct places right? Thanks for the help.

        ::ANSIScreen; use Win32::Console; use Win32::SysTray; no warnings 'numeric'; $| = 1; # ADDED 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 = Prompt("? "); # ADDED 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; }, ); } 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; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2024-04-23 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found