in reply to Re^3: Looping File transfer using SFTP in TK
in thread Looping File transfer using SFTP in TK

Yah thanks alot. I got a little of that before you sent it, but definitly not all of it. The program works great, does everything I want it to do. Now all that needs to be done is to make a listbox that prints all the print statements to it. It got some of it to work but when $msg=2 and the program enters the sub lstmsg, when it gets to the listbox insert the program stops. If I take the insert out it runs just fine. How can I fix this?
This is a snippet of just the subroutines with the omitted Tk widgets.
sub sub1 { if($btn->cget(-text) eq 'Connect'){ $go = 1; $btn->configure(-text=> 'Stop'); $msg = 1; lstmsg(1); } else{ $go = 0; $btn->configure(-text=> 'Connect'); } } sub lstmsg{ if($msg == 1){ $lstbx->insert('end',"Connecting... Asking for Password..."); #$lstbx->insert('end', "sftp connected $user $server"); } if($msg == 2){ print "huh\n"; $lstbx->insert('end', "sftp connected $user $server"); goto AFTER; } } sub sftp{ $|++; #now go into a waiting loop, where you wait for $go to be 1. when +$go = 1, the sftp will keep sending the file. #the only way to break out is to set $die=1 while(1){ if($die == 1){ goto END }; if ( $go == 1 ){ my $seconds = 120; my %args = (host=>$server, user=>$user, timeout=>$seconds) +; my $sftp = Net::SFTP::Foreign->new(%args); print "What\n"; $msg = 2; if($msg == 2){lstmsg(1)}; AFTER: print "happy\n"; for(;;){ $sftp->put("$local", "$remote"); print "fake file being sent\n"; sleep 2; if($go == 0){last}; if($die == 1){ goto END }; } undef $sftp; #close current sftp $go = 0; #turn off self before returning } else { sleep 1 } # sleep if $go == 0 } END: print "sftp stopped thread ending\n"; } sub clean_exit{ my @running_threads = threads->list; if (scalar(@running_threads) < 1){print "\nFinished\n";exit} else{ $die = 1; $thr->join; exit; } }

Replies are listed 'Best First'.
Re: Inserting Text into Listbox from a Thread
by zentara (Cardinal) on Apr 12, 2007 at 12:35 UTC
    You are making a big mistake in trying to access one thread from another. Remember the rules I stated for threaded-Tk apps. No Tk code in the thread. Don't try to access Tk widgets from the thread. And in your case, don't use a goto, to try to jump into a thread.

    Without doing the code for you, this is how you would do it.

    1. Make $msg, and $msg_return shared variables.

    2. Like I did with the file counter, setup a timer in the main thread to watch for the value of $msg. At the point where you test for $msg==2 in the thread, go into a wait-loop in the thread,waiting for the shared-value $msg_return == 1. When that happens, continue in the thread.

    3. When the main thread detects, $msg==2, it can do whatever is needed, then set $msg_return=1 and $msg=0, so the thread can continue.

    Remember, ALL Tk stuff has to be done in the main thread. All communication with the worker thread(s) has to be done through simple shared-variables.


    I'm not really a human, but I play one on earth. Cogito ergo sum a bum