[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Detecting thread exit immediately

Martin DeMello

4/8/2005 8:29:00 PM

From the fxirb code:

class FXIrb < FXText
include Singleton
include Responder

attr_reader :input

def FXIrb.init(p, tgt, sel, opts)
unless @__instance__
Thread.critical = true
begin
@__instance__ ||= new(p, tgt, sel, opts)
ensure
Thread.critical = false
end
end
return @__instance__
end

def create
super
setFocus
# IRB initialization
@inputAdded = 0
@input = IO.pipe
$DEFAULT_OUTPUT = self

@im = FXIRBInputMethod.new
@irb = Thread.new {
IRB.start_in_fxirb(@im)

#------------------------------
# INSERT CODE HERE
#------------------------------
}
end

# -------------------------------------------------------------

I want to put something in INSERT CODE HERE that will call a method in
the main thread when IRB.start_in_fxirb returns (i.e. just before the
other thread dies), rather than having to wait for the GC to collect the
the thread and then put in a check for @irb.alive?. Is there any good
way to do this? (What I'm trying to do is have some way for the fxirb
widget to notify the program containing it that the irb session has
ended. Right now my choices seem to be to call exit (exits everything),
to sleep(briefly) after I call @irb.run, and then check @irb.alive?
(annnoying), to call GC.start everytime I call @irb.run and then check
@irb.alive? (also annoying), or to have the thread itself notify
something when it dies (but I can't quite think of how to do it).)

martin

3 Answers

Joel VanderWerf

4/8/2005 8:54:00 PM

0

Martin DeMello wrote:
> I want to put something in INSERT CODE HERE that will call a method in
> the main thread when IRB.start_in_fxirb returns (i.e. just before the
> other thread dies), rather than having to wait for the GC to collect the
> the thread and then put in a check for @irb.alive?. Is there any good
> way to do this? (What I'm trying to do is have some way for the fxirb
> widget to notify the program containing it that the irb session has
> ended. Right now my choices seem to be to call exit (exits everything),
> to sleep(briefly) after I call @irb.run, and then check @irb.alive?
> (annnoying), to call GC.start everytime I call @irb.run and then check
> @irb.alive? (also annoying), or to have the thread itself notify
> something when it dies (but I can't quite think of how to do it).)

In line with your last thought, what about writing something to a queue
from the irb thread and then periodically (using a chore) reading the
queue in the main thread?


Martin DeMello

4/8/2005 9:07:00 PM

0

Joel VanderWerf <vjoel@path.berkeley.edu> wrote:
> > @irb.alive? (also annoying), or to have the thread itself notify
> > something when it dies (but I can't quite think of how to do it).)
>
> In line with your last thought, what about writing something to a queue
> from the irb thread and then periodically (using a chore) reading the
> queue in the main thread?

That still won't give me instant feedback, will it? Why I'm so keen on
this is that I want the FXIrb panel to stop accepting keystrokes the
instant the IRB interpreter exits. Everything I've tried so far either
exits the program, or has a brief time where you can type into a
theoretically exited widget (mostly since I was focusing on
Thread#alive? based solutions).

Is there any way I can have FXIrb pass in 'self' to the thread so that
it can call methods on it?

martin

Martin DeMello

4/8/2005 9:34:00 PM

0

Martin DeMello <martindemello@yahoo.com> wrote:
>
> Is there any way I can have FXIrb pass in 'self' to the thread so that
> it can call methods on it?

The good people on #ruby-lang solved this one :) It's as simple as

@irb = Thread.new(self) {
....
self.quit_method
}

martin