For your second question, there may be better ways to do what you want without blocking, but you don't show us what foo() does. Generally, you use fileevent with a piped open, to fork-off a long process, then use the fileevent to read the filehandle, and test for when it's done, or collect return values. You can also get more sophisticated, and use IPC::Open3 to be able to write to, and read from the forked process.
Since you are on windows, you might want to look at IPC::Run, which is known to work on win32.
There are plenty of examples here on perlmonks in the archives, or on google. Just search for "Tk fileevent" or "Tk IPC::Open3"
You can also use threads, to spawn a worker thread to run some code, but it is tricky to use with Tk (but doable). I don't know how well threads work on windows.
Here is a simple example to show threads working with Tk, on Linux. There are 2 basic rules to remember when using threads with Tk.
1. Create the threads first, and put them to sleep, before creating the Tk gui.
2. A thread must reach the end of it's code block, for it to return properly, thus the need for the goto in the code. ( Also check out Tk-with-worker-threads ).
#!/usr/bin/perl use strict; use Tk; use threads; use threads::shared; my $data_out:shared = 0; my $data_in:shared = 0; my $thread_die = 0; my $wthr = threads->new( \&update_thread )->detach; create_tk_window(); exit; ######################################################### sub update_thread { print "update_thread called...\n"; while (1) { if($thread_die == 1){goto END} $data_in = 'thread-processing'.$data_out; sleep 1; } END: } ######################################################### sub create_tk_window { my $mw = MainWindow->new( -background => 'black', -foreground => 'yellow', -title => "Thread Test" ); $mw->geometry("802x618"); $mw->minsize( 802, 618 ); $mw->maxsize( 802, 618 ); my $sent_recvd_listbox = $mw->Scrolled('Listbox', -height => 10, -width => 60, -background => 'black', -foreground => 'yellow' )->pack( -side => 'bottom', -anchor => 's', -pady => 2 ); my $server_list_listbox = $mw->Scrolled( "Listbox", -height => 10, -width => 60, -background => 'white', -foreground => 'black', -scrollbars => 'se', )->pack(); my $repeater; $mw->Button(-text=> 'Exit', -command => sub{ $thread_die = 1; $repeater->cancel; $mw->withdraw; kill 9, $$; })->pack; $repeater = $mw->repeat(1000, sub{ $data_out++; $server_list_listbox->insert( 'end', "Sent $data_out " ); $server_list_listbox->see('end'); $sent_recvd_listbox->insert( 'end', "Recived $data_in" ); $sent_recvd_listbox->see('end'); }); MainLoop; }
In reply to Re: Perl/Tk while {1} and a responsive UI
by zentara
in thread Perl/Tk while {1} and a responsive UI
by ady
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |