#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::Dialog; use threads; use threads::shared; my @sfiles = qw(numbers1 numbers2 numbers3 numbers4); # make threads first before any tk code my %shash; my %thash; my $numworkers = scalar @sfiles; my $searchnum; share $searchnum; $searchnum = '002-339-7473'; foreach my $dthread(1..$numworkers){ share ($shash{$dthread}{'go'}); share ($shash{$dthread}{'die'}); share ($shash{$dthread}{'file'}); share ($shash{$dthread}{'return'}); $shash{$dthread}{'go'} = 0; $shash{$dthread}{'file'} = shift @sfiles; $shash{$dthread}{'die'} = 0; $shash{$dthread}{'return'} = ''; $thash{$dthread}{'thread'} = threads->new(\&work,$dthread); } my $count = 1; my $mw = MainWindow->new; my $c = $mw->Canvas( -width =>400, -height=>200, -background => "white"); $c->pack; ############################Main window my $quit = $mw->Button( -text => "Quit", -padx => "8", -command => \&Quit); $quit->place(-x => 350, -y => 180); ###########################adds the quit button my $Find = $mw->Button( -text => "Search", -command => \&Search); $Find->place(-x => 300, -y => 180); ################################################ my $lab = $mw->Label ( -text => "Enter Phone Number", -background => "white" ); $lab->place(-x => 5, -y => 50); ##############################Label my $Entry = $mw->Entry( -textvariable => \$searchnum, -background => "white" ); $Entry->place(-x => 5, -y => 68); ##################################Entry my $text = $mw->Text( -height => 5, -width => 55, -wrap => 'char' ); $text->place(-x => 5, -y => 90); ################################# my $labWarning = $mw->Label ( -text => 'Example XXX - XXX - XXXX', -background => "white" ); $labWarning->place(-x => 140, -y => 68); my $index = 1; ####################################### sub Quit { my $d = $mw->Dialog( -text => "Are you sure you want to quit?", -buttons => ["Yes", "No"]); my $answer = $d->Show(); close(UNLOCK); #cleanup threads if($answer eq "Yes"){ for(1..$numworkers){ $shash{$_}{'die'} = 1; $thash{$_}{'thread'}->join; } exit; } } sub Search { my $found = 0; for(1..$numworkers){ $shash{$_}{'go'} = 1; } #setup a timer to watch for returns thru shared variables my $timer; # declare outside of block so you can kill it in callback $timer = $mw->repeat(10,sub{ for(1..$numworkers){ if( length $shash{$_}{'return'} > 0){ if( $shash{$_}{'return'} > 0 ){ my $message = "thread $_ detected number at line $shash{$_}{'return'} \n"; print "$message\n"; $text->insert('end',"$message\n"); } #stop other threads for(1..$numworkers){ $shash{$_}{'return'} = 0 } #stop timer $timer->cancel; } } }); } MainLoop; ####################################################################### sub work{ my $dthread = shift; $|++; my $file = $shash{$dthread}{'file'}; open(FH,"< $file") or die "$!\n"; print "thread $dthread created\n"; while(1){ if($shash{$dthread}{'die'} == 1){ goto END }; if ( $shash{$dthread}{'go'} == 1 ){ seek FH, 0, 0; #reset to top $shash{$dthread}{'return'} = ''; print "thread $dthread going\n"; while(my $line = ){ chomp $line; if($line eq $searchnum){ $shash{$dthread}{'return'} = __LINE__ ; print "$dthread found $shash{$dthread}{'return'}\n"; goto RESET; } if($shash{$dthread}{'go'} == 0){goto RESET} if($shash{$dthread}{'die'} == 1){ goto END }; } RESET: $shash{$dthread}{'go'} = 0; #turn off self before returning print "thread $dthread going back to sleep\n"; }else { select(undef,undef,undef,.1) } # tenth second sleep, can be smaller } END: }