http://www.perlmonks.org?node_id=1019329

ckant8 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have written a script to connect to host(win,unix) system and run some commands using a win32 GUI for entering commands. While it is in process the GUI is freezes and unresponsive until it completes it. Could anyone tell me the solution for this. Thanks ckant

Replies are listed 'Best First'.
Re: Win32 GUI freeze
by nikosv (Deacon) on Feb 18, 2013 at 12:37 UTC
    becuase the main (GUI) thread freezes. You either have to keep the message pump going by calling DoEvent (not recommended) or create a worker thread and do inter-threading communication. There are plenty of tutorials in the forum
      Thanks I have added parallel fork, but still no use. it is taking same time to execute and ui still hangs. Don't mine could you post any of the Perl Thread implementation example link for easy access, as i could not able to find proper one.
Re: Win32 GUI freeze
by eco (Acolyte) on Feb 18, 2013 at 12:57 UTC

    You basically have two options

    • Add a progress bar to show the computer is doing something, eventually letting the user cancel the task
    • Divide your task into subtasks which can be run from the event loop

    The first solution is the easiest to code

    -- ecocode
      I have added progress bar to my script. but gettinng error saying "can not call method SetRange on an undefined value". I have added use Win32::GUI(); my $progress_bar = $main->AddProgressBar ( -name => 'ProgressBar', -pos => 10, 10, -size => 270, 20, -smooth => 1, -vertical => 0, ); $progress_bar->SetRange(0,50); $progress_bar->SetStep($y); $progress_bar->StepIt();
Re: Win32 GUI freeze
by BrowserUk (Patriarch) on Feb 19, 2013 at 04:06 UTC

    Post your code and you'll get better answers.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      use thread; use Parallel::ForkManager; use Data::Dumper; use Net::Telnet; use Win32::API; use Win32::GUI(); $main = Win32::GUI::Window->new( -name => 'Main', -width => 500, -height => 900, -text => 'Perl', -minsize => 500, 900, ); my $loadImage = new Win32::API ('user32', 'LoadImage', 'N','N','I','I','I','I','N') or die 'cannot find LoadImage function'; my $waitCursor = $loadImage->Call(0, 32514, 2, 0, 0, 0x8040); my $font = Win32::GUI::Font->new( -name => "Comic Sans MS", -size => 8,); $main->AddLabel(-text => "Welcome ", -pos => 10, 10 , -size => 190, 125 ,); $main->AddLabel(-text => "Enter Host Name / IP : ", -pos => 10, 33 , -size => 190, 125 , ); my $t= $main->AddTextfield( -name => 'tfResults', -text => "hostname", -pos => 125, 30 , -size => 100, 25 , ); my $my_but = $main->AddButton(-text => "Submit",-name => 'btQuery', -pos => 235, 30 ,-size => 50, 25 ,); $main->Show(); Win32::GUI::Dialog(); sub btQuery_Click { $main-> DoEvents(); $txt1=$t->Text; if ($txt1) { my $txt1=$t->Text; if($txt1=~/a-zA-Z0-9+/) { #CHECKING WINDOWS OR UNIX HOST BY CALLING BELOW FUNCTION $oldCursor = Win32::GUI::SetCursor($waitCursor); #show hourglass ... print "\nConnecting to : $txt1"; &host1($txt1); if($hosttype=~/Unix/) { $t->ReadOnly(1); $my_but->Disable(); &unix_host; } elsif($hosttype=~/Windows/) { $t->ReadOnly(1); $my_but->Disable(); &win_host; } elsif($hosttype=~/unknowhost/) { $main->AddLabel(-text => "", -pos => 10, 200 , -size => 500, 500 ,); $main->AddLabel(-text => "Unable to connect host. Please check the host name/ip.", -pos => 10, 200 , -size => 500, 950 ,-font => $font, -foreground => 0, 0,250,); print "\nUnknow host. Please check the host name/ip."; last; } # Connecting to Unix host sub unix_host { my $txt1=$t->Text; $main->AddLabel(-text => "", -pos => 0, 200 , -size => 500, 900 ,); $main->AddLabel(-text => "Please wait.....logining to host. ", -pos => 10, 200 , -size => 500, 500 ,); $telnet = new Net::Telnet ( Timeout=>1800, Errmode=>'die', input_log=>'input.log',output_log=>'output.log',Dump_Log=>'dump.log',Option_log=>'option.log'); $telopt_ttype_ok = ''; $telnet->option_callback(\&opt_callback); $telnet->option_accept(Do=>Net::Telnet->TELOPT_TTYPE); $telnet->suboption_callback(\&subopt_callback); chomp $txt1; $telnet->open($txt1); my $oldCursor = Win32::GUI::SetCursor($waitCursor); #show hourglass ... $telnet->waitfor('/login: $/i'); $telnet->print('root'); $telnet->waitfor('/Password: $/i'); $telnet->print('dangerous'); $telnet->waitfor('/# $/i'); print "\nConnected to Unix host :$txt1 "; print FILE "\n@time1:Connected to Unix host: $txt1"; $main->AddLabel(-text => "Connected to Unix host. ", -pos => 10, 500 , -size => 500, 500 ,); #print "\nSetting symcli and adios path on host."; #my $PATH = '/usr/symcli/bin:/usr/adios'; #$telnet->print("PATH=$PATH"); #PATH=$PATH:/data/myscripts #$telnet->print("PATH=$PATH:/usr/symcli/bin:/usr/adios"); #$telnet->print("export PATH"); #$telnet->waitfor('/# $/i'); Win32::GUI::SetCursor($oldCursor); #show previous arrow cursor again $check1=$main->AddButton(-name => "Unix_Check",-text => "Check version",-pos => 55, 70 ,); $main->AddLabel(-text => " Enter custom cmd:",-pos => 10, 100 ,-size => 250, 125 ,); $t1= $main->AddTextfield(-name => 'tfResults2',-text => "",-pos => 130, 100 ,-size => 100, 25 ,); $submit=$main->AddButton(-text => "Submit",-name => 'Unix_submit',-pos => 250, 100 ,-size => 50, 25 ,); sub Unix_Check_Click { } sub Unix_submit_Click { } } } } }

        When you post code, you need to put it inside <code></code> tags, which makes it much easier for others to read.

        I've posted your code reformatted in a more easier to read format below. However, while reformatting your code, I noticed that it looks like there are some issues with curly brackets. There are some areas that are missing the closing curly brackets and then there are extra curly brackets at the end of your code.