Detecting thread exit immediately

M

Martin DeMello

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
 
J

Joel VanderWerf

Martin said:
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?
 
M

Martin DeMello

Joel VanderWerf said:
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
 
M

Martin DeMello

Martin DeMello said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top