For your first question of why windows won't autoload it, maybe you have unix line endings in the file? Try doing a unix2dos conversion on it.

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; }

I'm not really a human, but I play one on earth. flash japh

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.