Win32::GUI::ThreadUtilsコード入力をためす

別スレッド処理中にも、親スレッドへ結果を通知するパターン

use strict;
use warnings;
use utf8;
use Encode;
use threads;
use Win32::GUI();
use Win32::GUI::ThreadUtils;

my $DOS = Win32::GUI::GetPerlWindow();
Win32::GUI::Hide($DOS);

my $con = 1;

my $main = Win32::GUI::Window->new(
    -name   => 'Main',
    -width  => 240,
    -height => 100,
    -text   => encode( cp932 => "☆ほげほげ" ),
);

my $label1 = $main->AddLabel(
    -name  => 'LB1',
    -text  => " ",
    -width => 240,
);

my $label2 = $main->AddLabel(
    -name  => 'LB2',
    -text  => encode( cp932 => "出力" ),
    -width => 240,
    -pos   => [ 0, 60 ],
);

$main->AddButton(
    -name  => "Button1",
    -text  => encode( cp932 => "表示変更" ),
    -width => 80,
    -pos   => [ 0, 15 ],
);

$main->AddButton(
    -name  => "Button2",
    -text  => encode( cp932 => "実行" ),
    -width => 80,
    -pos   => [ 80, 15 ],
);

$main->Show();
Win32::GUI::Dialog();

sub Main_Terminate {
    -1;
}

sub Button1_Click {

    if ($con) {
        $label1->Change( -text => encode( cp932 => "こんばんわ" ) );
        $con = 0;
    }
    else {
        $label1->Change( -text => encode( cp932 => "こんにちわ" ) );
        $con = 1;
    }
    return 0;
}

sub Button2_Click {
    my $comms = $main->AttachComms( \&BossLog );
    my $thr = threads->create( \&worker, $comms );
    $thr->detach();
    return 0;
}

sub BossLog {
    my $self = shift;
    my $line = shift;
    $self->LB2->Change( -text => $line );
}

sub worker {
    my $SetProgress = shift;
    my $file        = 'test.log';
    open my $fh, '<', $file or die qq/Can't open file "$file" : $!/;
    while ( my $line = <$fh> ) {
        $SetProgress->Call($line);
        Win32::Sleep(20);
    }

}