Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

As metioned above, fork is emulated under Win32(using threads). So both branches of the fork are threads within the same process. When you then exec your command, the entire process is replaced by the exec'd command, which isn't very useful.

To avoid that problem, skip fork completely (just forking and doing nothing is enough to crash Tk;), and use system 1, 'wish temp.tcl'; to run the command asynchronously.

That works, but doesn't give you the opportunity to retrieve the return code from the command if that is important to you. If you need that, you can use threads::async, to run system synchronously in a thread, but asynchronously from the Tk thread.

The following snippet will run the command from the edit field via system in a thread, retrieve the return code and post it to the tk screen. The progress bar is there to show that the tk interface doesn't stall. Indeed, you can click 'Doit' several times and multiple copies of the command will run concurrently:

#! perl -slw use strict; use threads qw[ async ]; use Tk; use Tk::ProgressBar; use Tk::ROText; my $cmd = 'perl -le" print for 1 .. 10; sleep 10; exit 123"'; my $mw = MainWindow->new; my $pb = $mw->ProgressBar()->pack(); my $txt = $mw->ROText( -width => 3, -height => 1, )->pack( -side => 'left' ); my $edit = $mw->Entry( -width => 80, -textvariable => \$cmd )->pack( -side => 'left' ); my $btn = $mw->Button( -text => 'Doit', -command => sub{ async{ system $cmd; $txt->Contents( $? >> 8 ); }; } )->pack( -side => 'left' ); my $progress = 0; my $repeat = $mw->repeat( 100 => sub{ $progress = 0 if ++$progress == 100; $pb->value( $progress ); } ); MainLoop;

When you terminate the tk window, you will see the following errors displayed:

Use of uninitialized value in hash element at c:/Perl/site/lib/Tk/Trac +e.pm line 181. Attempt to free non-existent shared string 'Modified' during global de +struction.

The first I have no idea about but is probably related to the second.

The second is a consequence of using tk and threads together, but it only occurs when the app is terminating anyway and does not appear from my scant tests to indicate anything that would prevent the app from running correctly and completing successfully.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

In reply to Re: panic: restartop error using Perl Tk + fork() on Win32. by BrowserUk
in thread panic: restartop error using Perl Tk + fork() on Win32. by jryan

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found