use threads; use threads::shared; my $KillThread : shared; my $FunctionName : shared; my $ThreadWork : shared; my @ArgumentsThread : shared; my $RefResultFunction : shared; $ThreadWork = 0; $KillThread = 0; my %FunctionsToLaunchThread = ( "run_script" =>\&run_script ); # Creation of thread my $Thread = threads->create( \&WorkThread ); sub load { ......some code...... $run_button->configure(-command =>[ (\&runtk, $paste_text)],-state => 'normal'); ......some code.... } sub runtk { my $temp = $_[0], $FunctionName = "run_script"; @ArgumentsThread = ($temp); $ThreadWork = 1; $run_button->configure(-state => "disabled"); while ( $ThreadWork == 1 ) { sleep 0.2; $f->update; } $run_button->configure(-state => "normal"); } sub run_script{ open (MYFILE1, "subroutines.txt"); my @sub_lines = ; my $current = localtime(time); #print "Current date and time is $current"; use POSIX qw(strftime); my $now_string = strftime "%d-%b-%Y--%T", localtime; print $now_string; my $str ="$now_string.txt"; print $str; open FILE, "+>>Test_Results/$str" or die $!; do "add.pl"; use Device::Modem::GSM; my $modem = new Device::Modem::GSM( port => "/dev/$port_global",log => "file,$now_string.log", loglevel => 'debug', # default is 'warning' ); sub WorkThread { while (1) { if ( $ThreadWork == 1 ) { $FunctionsToLaunchThread{$FunctionName}->@ArgumentsThread); $ThreadWork = 0; } last if ( $KillThread == 1 ); sleep 0.5; } return; }