in reply to Re: Re: Re: Threads problem.
in thread Threads problem.

Dear Friends,

Ok this what I have done; it seems to work up to the point were I engage the threading routines. I get a hell load of Tk related errors - although, the TK side of thing works fine without the threading being involved!

All my troubles are in the size_info sub - the Tk progress bit at the beginning, which also doesn’t work here but works fine on its own in another example script – therefore your help, enlightening comments (or code) and divine guidance are highly appreciated.



Thanks in advance
use strict; use warnings "all"; use diagnostics; use Win32::Lanman; use Win32::OLE qw[ in with ];; use Win32; use Tk; use Tk::ProgressBar; use threads qw[ yield ]; use threads::shared; use Thread::Queue; use POSIX qw[_exit]; use vars qw/%Data %Tk %App %threads @ShareLst/; my $Qwork = new Thread::Queue; my $Qresults = new Thread::Queue; my $max = $ARGV[0] || 20; sub syntax { $Data{script} = $0; my ( $line ) = "^" x length ( $Data{script} ); system ("CLS"); print <<EOT; User requested help or invalid operating option(s) were detected. $Data{script} $line syntax......lablabla EOT exit( ); } sub get_shares { $Data{count1} = 0; print "\n.....Please wait\n"; if (Win32::Lanman::NetShareEnum($Data{server}, \ my @EnumLst)) { for my $Share (@EnumLst) { next if ( ($Share->{path}) =~ /^c|^[d-z]:\\$/i); next if ( ($Share->{netname})=~ /ipc\$|rpc\$|netlogon\$|ad +min\$|^CDROM|^\w\$/i); $~ = "F1"; write; push (@ShareLst, $Share); format F1 = @<<<<<< @<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< +<<<<<<<<<<< ++$Data{count},$Share->{netname},$Share->{path} . } print "\nTotal number of shares = $Data{count} (out of $#EnumL +st)\n"; } else { Win32::MsgBox(Win32::Lanman::GetLastError(),31,"Share Access E +rror"); syntax(); } } sub prep_OLE { $App{row} =1; $App{col} = 1; $App{name} = 'Excel.Application'; $App{xls} = Win32::OLE -> GetActiveObject ( $App{name} ) || Win32: +:OLE -> new ( $App{name} ); die "\nCannot start $App{name}! : $!\n" unless $App{xls}; $App{xls}->{Visible} = 1; $App{book} = $App{xls} -> Workbooks -> Add ( ); $App{sheet} = $App{xls}-> Worksheets ( "Sheet1" ); $App{sheet}-> Columns ("A:A")->{ColumnWidth} = "20"; $App{sheet}-> Cells(1,1)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,1)->{value}='Share Net Name'; $App{sheet}-> Columns ("B:B")->{ColumnWidth} = "35"; $App{sheet}-> Cells(1,2)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,2)->{value}='Path'; $App{sheet}-> Columns ("C:C")->{ColumnWidth} = "15"; $App{sheet}-> Cells(1,3)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,3)->{value}='Size'; $App{sheet}-> Columns ("D:D")->{ColumnWidth} = "15"; $App{sheet}-> Cells(1,4)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,4)->{value}='Num of Files'; $App{sheet}-> Columns ("E:E")->{ColumnWidth} = "15"; $App{sheet}-> Cells(1,5)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,5)->{value}='Num of Folders'; $App{sheet}-> Columns ("F:F")->{ColumnWidth} = "30"; $App{sheet}-> Cells(1,6)->Font->{FontStyle}='bold'; $App{sheet}-> Cells(1,6)->{value}='Remarks'; $App{xls}-> ActiveWorkbook -> SaveAs( $Data{server}); } sub worker { my $fso = Win32::OLE->new( 'Scripting.FileSystemObject' ); Win32::OLE->Option( Warn => 0 ); while( my $folder = $Qwork->dequeue ) { last if $folder eq 'DIE_NOW'; my @folders = $fso->GetFolder( $folder ); my $size = $folders[0]->size() || 'Permission denied'; my( $cFiles, $cFolders ) = (0) x 2; while( my $sub = pop @folders ) { $cFiles += $sub->Files->Count || 0; $cFolders += $sub->SubFolders->Count || 0; for my $subsub ( in $sub->SubFolders ) { $cFolders += $subsub->Files->Count || 0; push @folders, $_ for in $subsub->SubFolders; $cFolders += $subsub->SubFolders->Count || 0; } } $Qresults->enqueue( "$folder - size: $size Files: $cFiles Fold +ers: $cFolders" ); yield unless $Qwork->pending; } undef $fso; return 1; } sub size_info { print"\nRetrieving size information;\n\n"; my $Share; $App{row} =2; $Data{count2} =0; my $NumofShr = 1 + $#ShareLst ; while (@ShareLst) { #$Tk{MW}= MainWindow->new (-title => "$Data{server}"); #$Tk{UpperFrame} = $Tk{MW}->Frame; #$Tk{UpperFrame}->pack (qw/-side top -fill both -padx 5 -pady +5/); #$Tk{progress}->pack(-fill => 'x'); my @PathLst = (); for (1..$max) { last if ($Data{count2} == $NumofShr); $Share = shift @ShareLst; $App{col}=1; $App{sheet}-> Cells ($App{row}, $App{col}++)-> {value}=$Sh +are->{netname}; $App{sheet}-> Cells ($App{row}++, $App{col}++)-> {value}=$ +Share->{path}; $Share->{path} =~ s/:/\$/; my $Target = "\\\\" . $Data{server} . "\\" . $Share->{path +}; print ++$Data{count2} . ") $Target\n"; push (@PathLst, $Target); } my @threads = map{ threads->new( \&worker ) } 1 .. @PathLst; $Qwork->enqueue( @PathLst ); yield until $Qresults->pending(); for( 1 .. @threads ) { print $Qresults->dequeue; } $Qwork->enqueue( 'DIE_NOW' ) for 1 .. @threads; _exit 0; } } $Tk{MW}= MainWindow->new (-title => "$0: Processing "); $Tk{UpperFrame} = $Tk{MW}->Frame; $Tk{LowerFrame} = $Tk{MW}->Frame; $Tk{FieldName} = $Tk{UpperFrame}->Label(-text=>'Server Name'); $Tk{Entry} = $Tk{UpperFrame}->Entry( -textvariable=> \ $Data{server}); $Tk{GoButton} = $Tk{LowerFrame}->Button( -text=>'Go', -borderwidth=>5, -width=>10, -command=>sub { syntax( ) if ($Data{server} eq "") +; system("cls"); + print "\n\nObtaining $Data{server} + share information.\n\n"; $Tk{MW}->destroy; get_shares; prep_OLE; + size_info; $App{xls}-> ActiveWorkbook -> Save +As( $Data{server}); Win32::MsgBox("Operation completed + successfully",32,"Share Info"); exit( ); }); $Tk{progress} = $Tk{UpperFrame}->ProgressBar( -length => 200, -width => 20, -anchor => 'w', -borderwidth => 1, -highlightthickness = +> 1, -from => 0, -to => 100, -blocks => 100, -colors => [ 0, 'navy', + ], -variable => \$Tk{percen +t_done}); $Tk{UpperFrame}->pack (qw/-side top -fill both -padx 5 -pady 5/); $Tk{LowerFrame}->pack (qw/-side bottom -fill both -padx 5 -pady 5/); $Tk{FieldName}->pack (qw/-side left/); $Tk{Entry}->pack (qw/-side right/); $Tk{GoButton}->pack (qw/-side bottom -fill both -padx 5 -pady 5/); MainLoop; exit 1;
Thanks in advance

Humbled by your knowledge,