# Calling a method in the context of a specific thread

**URL:** <https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725>\
**Category:** Ruby\
**Created:** [September 29, 2010, 3:23am UTC](https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725 "2010-09-29T03:23:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 29, 2010, 3:23am UTC](https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725/1 "2010-09-29T03:23:28Z")

</div>

Background:

I am writing an FXRuby program. My question should be independent of  
FXRuby, though.

FXRuby is a GUI for Ruby.

If I understand some of the comments made here (and I probably don’t)  
[see subject: interpreter cannot enter in the context from another  
thread] it is a bad thing to attempt to run an FXRuby function in a  
non-main thread.

So … I have a non-main thread that wants to display a message box  
(i.e. “call” FXMessageBox").

How can I invoke FXMessageBox in the context of the main thread?

I can think of all sorts of ugly solutions.

For instance:

1. Set some global\_variable indicating the task I want to perform in the  
main thread.
2. Do a thread.stop
3. have a timer in the main thread and have it dispatch based on  
global\_variable
4. save the return code of FXMessageBox in a thread local variable the  
not-main-thread
5. Do a thread.run to wake up the stopped not-main-thread.

Ugly!

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 30, 2010, 8:14am UTC](https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725/2 "2010-09-30T08:14:25Z")

</div>

On 09/29/2010 03:22 AM, Ralph S. wrote:

> For instance:
> 
> 1. Set some global\_variable indicating the task I want to perform in the main thread.
> 2. Do a thread.stop
> 3. have a timer in the main thread and have it dispatch based on global\_variable
> 4. save the return code of FXMessageBox in a thread local variable the not-main-thread
> 5. Do a thread.run to wake up the stopped not-main-thread.

1. Have a task queue which is read in the main thread. You can even  
store lambdas in there.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 30, 2010, 2:27pm UTC](https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725/3 "2010-09-30T14:27:18Z")

</div>

Robert,

Thursday, September 30, 2010, 12:10:34 AM, you wrote:

RK\> On 09/29/2010 03:22 AM, Ralph S. wrote:

> > [Note: parts of this message were removed to make it a legal post.]

> > Background:

> > I am writing an FXRuby program. My question should be independent of FXRuby, though.

> > FXRuby is a GUI for Ruby.

> > If I understand some of the comments made here (and I probably don’t) [see subject: interpreter cannot enter in the context from another thread] it is a bad thing to attempt to run an FXRuby function in a non-main thread.

> > So … I have a non-main thread that wants to display a message box (i.e. “call” FXMessageBox").

> > How can I invoke FXMessageBox in the context of the main thread?

> > I can think of all sorts of ugly solutions.

> > For instance:

> > 1. Set some global\_variable indicating the task I want to perform in the main thread.
> > 2. Do a thread.stop
> > 3. have a timer in the main thread and have it dispatch based on global\_variable
> > 4. save the return code of FXMessageBox in a thread local variable the not-main-thread
> > 5. Do a thread.run to wake up the stopped not-main-thread.

RK\> 6) Have a task queue which is read in the main thread. You can even  
RK\> store lambdas in there.

Please explain more; a lot more.

I am particularly confused how a task queue would work given that  
nothing in the main thread can block since that would stop FXRuby’s  
message pump.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [September 30, 2010, 3:17pm UTC](https://www.ruby-forum.com/t/calling-a-method-in-the-context-of-a-specific-thread/195725/4 "2010-09-30T15:17:05Z")

</div>

On Thu, Sep 30, 2010 at 2:26 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > 1. Do a thread.run to wake up the stopped not-main-thread.
> 
> RK\> 6) Have a task queue which is read in the main thread. You can even  
> RK\> store lambdas in there.
> 
> Please explain more; a lot more.
> 
> I am particularly confused how a task queue would work given that nothing in the main thread can block since that would stop FXRuby’s message pump.

Actually I do not know RXRuby - I just answered based on the  
information in your posting. Maybe there is a queue in FXRuby that is  
regularly read from the main thread and which you can use. Actually  
you need to be able to interfere with FXRuby’s processing otherwise I  
don’t understand how you want to make option 1 work.

Btw, a quick Google revealed this at the top of the list:

[http://www.fxruby.org/doc/differences.html#d0e5063](http://www.fxruby.org/doc/differences.html#d0e5063)

Kind regards

robert
