<?xml version="1.0" encoding="windows-1252"?>
<node id="1013876" title="Re^5: Win32::GUI sending Windows messages between threads" created="2013-01-17 15:07:35" updated="2013-01-17 15:07:35">
<type id="11">
note</type>
<author id="171588">
BrowserUk</author>
<data>
<field name="doctext">
&lt;blockquote&gt;&lt;i&gt;&lt;/i&gt;&lt;/blockquote&gt;

&lt;p&gt;The problem is that you are tryiong to hook into a system/module defined API without understanding the rules and requirements of that API. 

&lt;p&gt;Each window can have multiple timers associated with it. This, when the timer triggers and it sends the WM_TIMER message, it *must* also send some additional information as a part of that message that identifies &lt;i&gt;which timer routine&lt;/i&gt; should be invoked to handle it. This is easily demonstrated by the following modification of your code which sets up two independent timers for the main window::&lt;spoiler&gt;&lt;code&gt;
#!perl
use Data::Dump qw[ pp ];
use threads;
use Win32::GUI qw( WM_CLOSE WM_TIMER );

#$CHILD = threads-&gt;create('child', '');

$WinMain = Win32::GUI::Window-&gt;new(
    -name =&gt; 'Main',
    -text =&gt; 'Task Test',
    -width =&gt; 300,
    -height =&gt; 200,
    -onTerminate =&gt; \&amp;Main_Terminate,
    -onTimer =&gt; \&amp;DoTimer,
);

my $timer1 = $WinMain-&gt;AddTimer( 'Timer1', 10000 );
pp $timer1;
my $timer2 = $WinMain-&gt;AddTimer( 'Timer2', 20000 );
pp $timer2;

$WinMain-&gt;Show();
Win32::GUI::Dialog();
$CHILD-&gt;detach();

sub Main_Terminate { -1; }

sub child {
    while( sleep 1 ) {
        Win32::GUI::PostMessage( $WinMain, WM_TIMER, 1, 'Timer1' );
    }
}

sub DoTimer {
    pp \@_;
    print "Timer fired\n";
    return 1;
}

&lt;/code&gt;&lt;/spoiler&gt;

&lt;p&gt;Which when run produces the following output:&lt;code&gt;
C:\test&gt;\perl32\bin\perl 1013733.pl
bless({ "-handle" =&gt; 3213678, "-id" =&gt; 1, "-interval" =&gt; 10000, "-name" =&gt; "Timer1" }, "Win32::GUI::Timer")
bless({ "-handle" =&gt; 3213678, "-id" =&gt; 2, "-interval" =&gt; 20000, "-name" =&gt; "Timer2" }, "Win32::GUI::Timer")
[
  bless({
    # tied Win32::GUI::Window
    "-accel"  =&gt; 0,
    "-handle" =&gt; 3213678,
    "-name"   =&gt; "Main",
    "-timers" =&gt; { 1 =&gt; "Timer1", 2 =&gt; "Timer2" },
    "-type"   =&gt; 0,
    Timer1    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 1, "-interval" =&gt; 10000, "-name" =&gt; "Timer1" }, "Win32::GUI::Timer"),
    Timer2    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 2, "-interval" =&gt; 20000, "-name" =&gt; "Timer2" }, "Win32::GUI::Timer"),
  }, "Win32::GUI::Window"),
  "Timer1",
]
Timer fired
[
  bless({
    # tied Win32::GUI::Window
    "-accel"  =&gt; 0,
    "-handle" =&gt; 3213678,
    "-name"   =&gt; "Main",
    "-timers" =&gt; { 1 =&gt; "Timer1", 2 =&gt; "Timer2" },
    "-type"   =&gt; 0,
    Timer1    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 1, "-interval" =&gt; 10000, "-name" =&gt; "Timer1" }, "Win32::GUI::Timer"),
    Timer2    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 2, "-interval" =&gt; 20000, "-name" =&gt; "Timer2" }, "Win32::GUI::Timer"),
  }, "Win32::GUI::Window"),
  "Timer2",
]
Timer fired
[
  bless({
    # tied Win32::GUI::Window
    "-accel"  =&gt; 0,
    "-handle" =&gt; 3213678,
    "-name"   =&gt; "Main",
    "-timers" =&gt; { 1 =&gt; "Timer1", 2 =&gt; "Timer2" },
    "-type"   =&gt; 0,
    Timer1    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 1, "-interval" =&gt; 10000, "-name" =&gt; "Timer1" }, "Win32::GUI::Timer"),
    Timer2    =&gt; bless({ "-handle" =&gt; 3213678, "-id" =&gt; 2, "-interval" =&gt; 20000, "-name" =&gt; "Timer2" }, "Win32::GUI::Timer"),
  }, "Win32::GUI::Window"),
  "Timer1",
]
Timer fired
&lt;/code&gt;


&lt;p&gt;That suggests the possibility that the (Win32::GUI defined) window procedure arranges for the system defined timer to include a numerical 'timer id' as a part of the WM_TIMER message. It then looks up that id in the -timers hash associated with the target window to obtain the &lt;i&gt;name&lt;/i&gt; of the blessed timer object, and then uses that to obtain the information passed onto the -onTimer callback routine.

&lt;P&gt;I've tried passing various combinations of id and name on the SendMessage WM_TIMER caller, but have yet to find the correct combination to allow me to trigger tha callback. I suspect that if you dug around in the Win32::GUI sources -- the only reliable documentation I have found for that module -- you might be able to discover the correct magic to allow you to manually trigger the callback, but sending a bare WM_TIMER will not do it. And is not intended to do so.


&lt;div class="pmsig"&gt;&lt;div class="pmsig-171588"&gt;
&lt;hr /&gt;
&lt;font size=1 &gt;
&lt;div&gt;With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'&lt;/div&gt;
&lt;div&gt;Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.&lt;/div&gt;
&lt;div&gt;"Science is about questioning the status quo. Questioning authority". &lt;/div&gt;
&lt;div&gt;In the absence of evidence, opinion is indistinguishable from prejudice.
&lt;/div&gt;
&lt;/font&gt;

&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1013733</field>
<field name="parent_node">
1013839</field>
</data>
</node>
