Tk, is not threadsafe, but you can still use threads with it. Your problem comes from how you are calling the sub Rundibbler. If you call it from a Tk callback, like a Button, timer, etc, it will cause that error. The reason is that when threads get created they get a copy of the parent, at the time of creation. If you are calling it from a Tk callback, or after any Tk statements are in your script, some of the Tk code gets into the thread. Since Tk is NOT threadsafe, everything gets confused, as the Tk code in the parent and thread interfere with each other.You can use the Super Search here and search for "Tk threads" and find the same advice given. Basically you need to create your threads BEFORE any Tk code is invoked. That usually means you create the threads and have them in a wait loop for a command(thru shared variables) to start running.Also you cannot try to access a Tk widget from a thread( as in your $reply->insert()), you must juggle shared variables in the main thread, and let the main thread adjust the widgets.
You can try , as a test, to just print to stdout, instead of your thread's $reply->insert();
and see how long it will run. It may well run for awhile, then unexpectedly crash, due to the first reason I mentioned above. BUT if it runs fine, you can share filehandles between threads.
#in main Tk thread
my $fileno = $shash{'fileno'}; #from a shared variable set in thread
print "fileno_m $fileno\n";
open (FH, "<&=$fileno") or warn "$!\n";
# filevent works but may not work on win32
$mw->fileevent(\*FH, 'readable', [\&fill_text_widget,$text]);
#in thread code block
my $pid = open(FH, "top -b |" ) or warn "$!\n";
my $fileno = fileno(FH);
$shash{'fileno'} = $fileno;
To expand your horizons a bit, Perl/Gtk2 has some thread-safety built in, and is easier to use with threads; but it is not perfect, and you can get similar errors with Gtk2.
| [reply] [d/l] |