- 论坛徽章:
- 0
|
用rob大侠的代码很好用,但为什么刚开始每个数字出现两次呢,继续运行好像才会一次?
谢谢哪位老师解答。
- use strict;
- use threads 1.39;
- use threads::shared;
- use Thread::Queue;
- use Win32::GUI ();
- # 最大线程数
- my $Max :shared = 2;
- # 退出程序标记
- my $Quit :shared = 0;
- # 线程开始标记
- my $Start :shared = 0;
- # 线程退出标记
- my $Stop :shared = 0;
- # 创建一个队列,线程间数据交换
- my $q = Thread::Queue->new();
- # 预建一个线程,用于线程管理
- threads->create('waiter')->detach;
- # GUI事件循环内不要放耗费CPU时间的操作
- # 可以考虑将GUI会话单独放入一个线程
- #create main window
- my $main = Win32::GUI::Window->new(
- -name => 'Main',
- -width => 410,
- -height => 220,
- -text => 'test',
- -maxsize => [410, 220],
- -minsize => [410, 220],
- );
- $main->AddTextfield(
- -name => "Text_Informa",
- -left => 10,
- -top => 80,
- -width => 383,
- -height => 100,
- -multiline => 1,
- );
- $main->AddButton(-name => 'btnDown', -text => 'Download',-left => 265, -top => 45,);
- $main->AddButton(-name => 'btnStop', -text => 'Stop',-left => 338, -top => 45,);
- # initialization
- my $w = $main->ScaleWidth();
- my $h = $main->ScaleHeight();
- my $desk = Win32::GUI::GetDesktopWindow();
- my $dw = Win32::GUI::Width($desk);
- my $dh = Win32::GUI::Height($desk);
- my $x = ($dw - $w) / 2;
- my $y = ($dh - $h) / 2;
- $main->Move($x, $y);
- $main->Show();
- $main->AddTimer('T1', 100);
- Win32::GUI::Dialog();
- exit(0);
- sub waiter
- {
- while (!$Quit) {
- for (my $num = $Max - threads->list(); $num && $Start; $num--) {
- threads->create('mythread');
- }
- sleep 1;
- }
- }
- sub mythread {
- my $pg = 9;
- while($pg > 1){
- $q->enqueue($pg);
- sleep 1;
- if($Stop == 1){last;}
- $pg--;
- }
- threads->detach();
- threads->exit();
- }
- # window event handler
- sub Main_Terminate {
- $main->Hide();
- $Quit = $Stop = 0;
- sleep 1;
- -1; #terminate the message loop
- }
- sub Main_Resize {
- $main->Resize($main->ScaleWidth - 20, $main->ScaleHeight - 40);
- }
- sub btnDown_Click {
- #my $thr = new threads(\&mythread );
- #$thr->detach();
- $Start = 1;
- $Stop = 0;
- }
- sub btnStop_Click {
- $Stop = 1;
- $Start = 0;
- }
- sub T1_Timer
- {
- return unless $q->pending();
- my $p = $q->dequeue_nb();
- $main->{Text_Informa}->Append($p . "\n");
- }
- #sub EndIt {my $con=shift; <>; $$con=1; return; }
- #参考自[[url]http://topic.csdn.net/t/20050503/16/3981999.html#[/url][/url]
- #sub mythread1 {my $thd=new threads("EndIt",\$control); $thd->join(); }
复制代码
[ 本帖最后由 3chi01 于 2009-2-10 09:18 编辑 ] |
|