I am trying to use the Win32::GUI module to handle popup menus with a system tray icon. The problem is that when I give the menu items callbacks to execute when clicked, the callbacks don't execute until TrackPopupMenu is called a second time. I don't understand why this is happening. Here is a simple example:
use Win32::GUI (); my $window = Win32::GUI::Window->new(); my $menu = Win32::GUI::Menu->new(-name => "TestMenu"); $menu->{"TestMenu"}->AddMenuItem( -item => 0, -id => 14, -text => "Exit", -name => "randomName", -onClick => sub { exit; }, ); #$window->SetForegroundWindow(); $window->TrackPopupMenu($menu->{"TestMenu"}, 20, 20); #$window->PostMessage("Msg", 0, 0); #$window->TrackPopupMenu(Win32::GUI::Menu->new(-name => "dummyMenu"), +20, 20); while (1) { sleep(1); }
Notice that while the second TrackPopupMenu call is commented, the subroutine for the menu is not called. Uncomment the second call to $window->TrackPopupMenu(...) to see how the second call will trigger the subroutine that should have executed immediately after the first call. Adding in SetForegroundWindow/PostMessage doesn't help. If someone knows how to make the subroutine get called without a second call to TrackPopupMenu I would appreciate it. Thanks!

In reply to TrackPopupMenu delay by p1ato

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.