Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Tk buttons: Run/Cancel

by gri6507 (Deacon)
on Aug 13, 2003 at 17:03 UTC ( [id://283620]=perlquestion: print w/replies, xml ) Need Help??

gri6507 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I believe I have seen this question posted here some time ago, but cannot find a link to it now. I have an Tk app that had two buttons: Run and Cancel. Normally, the Run button is "normal" and the Cancel is "disabled". When Run is clicked, the button states change. However, since the Run callback takes a long time to execute, the Cancel button does not function (the desired behavior is to break out of the Run callback and change the button states back, but I can't even get it to print "hello"). It's as though I need to display the Cancel button in a seperate thread.

Please help. Thanks.

Replies are listed 'Best First'.
Re: Tk buttons: Run/Cancel
by converter (Priest) on Aug 13, 2003 at 17:31 UTC
      It looks like this module is close to what I need. But this module executes a "system" call. I need to execute a routine. I don't think IO::Handle works for my situation. And doesn't Fork() have to wait for the child to end anyways? Any suggestions?
Re: Tk buttons: Run/Cancel
by meredith (Friar) on Aug 13, 2003 at 17:35 UTC
    Here's what I would do:

    In your "Cancel" callback, add a line that creates a file somewhere, say "/tmp/myprogram-cancel", and returns.

    In your "Run callback", fork your app, and add checks for something like
    if (-f "/tmp/myprogram-cancel") { unlink "/tmp/myprogram-cancel"; exit; }
    If you're not familiar with this technique, it's called a semaphore file, and there are numerous other ways to get a similar result too! You'll probably get a lot of responses from the monks on this one :).

    Eh, you will likely also want to use another semaphore or check to make sure that your user doesn't click Run multiple times with this one! :)

    Update: Do look at that Tk::ExecuteCommand, it greatly simplifies doing what you want! TIMTOWTDI (did I get that right?)

    HTH

    mhoward - at - hattmoward.org
      I have thought of this method (or something similar, using a global variable). However, I don't think this method would work here. My Run callback is something like this:

      foreach(@list){ DoLongBlockingThing($_); }

      so, I would need the cancel button to immediately break the LongBlockingThing() routine and not have to wait for the loop to come around and check the presence of the temp file (or variable).

        Well, noting your reply above also, I would fork and store the child pid somewhere (file or well-scoped variable), then have your cancel button shoot a kill at it. :) would this solve your problem?

        mhoward - at - hattmoward.org
Re: Tk buttons: Run/Cancel
by RMGir (Prior) on Aug 13, 2003 at 17:58 UTC
    Tk::ExecuteCommand does look like a good solution, as others have suggested.

    I'm playing with POE this week, so I'll toss in an alternative suggestion: Using Tk with POE.
    --
    Mike

Re: Tk buttons: Run/Cancel
by Anonymous Monk on Jan 20, 2004 at 16:19 UTC
    Hello,
    I had a similar problem recently. The suggestions made here were not fitting for me since I live under Win32.

    I solved it roughly that way:
    1. The "Cancel"-Button sets $abort=1;.
    2. The LongRun-Routine behind my "Run"-button:
    sub runlong { # <== Maybe set local grab here for cancel-button my $t=time()+30; while(time()<$t) # loop a loong time =8] { # Insert what ever need here # $mw->update(idletask); # Refresh Window DoOneEvent(0); # Simulate Mainloop. last if $abort != 0; # Bail out on User interaction } # <== Maybe remove grab here, if set above $abort=0; }
    - Steppl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://283620]
Approved by Thelonius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found