# wxRuby dialog separate from main Ruby script execution

**URL:** <https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789>\
**Category:** wxRuby\
**Created:** [January 15, 2009, 11:29am UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789 "2009-01-15T11:29:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 15, 2009, 11:29am UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/1 "2009-01-15T11:29:18Z")

</div>

Hi,

We have quite a large codebase in Ruby which uses Log4r to log errors,  
warnings etc. I have been trying to create a log window dialog using  
wxRuby that can be popped up if a piece of code generates an error or  
warning in one of our scripts.

E.g.  
GUI::LogWindow::show\_dialog( ) do  
# Do something that will log messages.  
end

def LogWindow::show\_dialog( log, auto\_popup = false, title = ‘Log’,  
&block )  
log\_window\_thread = Thread.new do  
begin  
LogDialogApp.new( log, auto\_popup, title ).main\_loop( )  
rescue Exception =\> ex  
throw unless ( ‘exit’ == ex.message )  
end  
end

yield if ( block\_given? )  
log\_window\_thread.join( )  
end

My current problem is that when the wxRuby’s main\_loop is invoked no  
Ruby code gets executed. Is there any way around this? Or is this a  
limitation of using wxRuby?

Any suggestions appreciated.  
Cheers,  
David

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 15, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/2 "2009-01-15T12:58:55Z")

</div>

Well, given the example, it’s kinda confusing how you are setting this  
Log  
Window class up, and how things are truely initializing. First of all,  
you  
really should be starting the main\_loop at the top level of your code,  
not  
inside a Window class method, even if it’s a module/singleton method.

Second, if your using threads, you need to setup a point for wxRuby to  
break  
from it’s processing of code internally in the main\_loop, to yield back  
control to Ruby under Ruby 1.8. Ruby 1.9 will work perfectly fine, as  
the  
threads are system native threads, not Green threads as in 1.8.

Third, if you have a single point of entry into your own application,  
that  
starts everything off, it would be best to call that method inside the  
on\_init of your App instance inside a thread, and then do the previous  
method of yielding control back to ruby from wxRuby. You can yield the  
control back to Ruby, by doing Wx::Timer.every(55) { Thread.pass }.  
This  
will execute a timer every 55 milliseconds, that will call Thread.pass  
to  
give the next thread a bit of the CPU Scheduling, before returning back  
to  
the wxRuby main\_loop.

Lastely, if you don’t have a single point of entry into your  
application,  
then I would suggest making one, and doing as I described above, as this  
will make things easier to track, should something go wrong.

hth,

Mario

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 15, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/3 "2009-01-15T13:23:49Z")

</div>

Hi,

Thanks for the swift responses guys.

The reason I don’t have a single point of entry, and spawned a thread  
for the UI, is because I want this as a bolt on to existing scripts.

Such that existing scripts can be run with displaying a log when the  
end-user runs it or not displaying a UI when an automated process is  
running the script(s).

Having said that I’ll have a go at refactoring the log window taking  
your comments on board, and checking other threads, to see if I can work  
something out.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 15, 2009, 3:30pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/4 "2009-01-15T15:30:56Z")

</div>

David Muir wrote:

> The reason I don’t have a single point of entry, and spawned a thread  
> for the UI, is because I want this as a bolt on to existing scripts.
> 
> Such that existing scripts can be run with displaying a log when the  
> end-user runs it or not displaying a UI when an automated process is  
> running the script(s).

I don’t know Log4r but you can define custom Loggers etc. Define one  
that call Wx::log\_message, Wx::log\_warn etc in response to the Log4r  
calls in your core file. Your UI shell is going to look something like  
the structure below:

hth  
alex  
\_\_

require ‘wx’

### Custom outputter /logger here

# What to run

scripts = [‘foo.rb’]

Wx::App.run do  
Wx::log\_message(‘Starting’)  
t = Thread.new do  
scripts.each do | script |  
Wx::log\_message(“Running #{script}”)  
load script  
end  
end

Wx::Timer.every(25) { Thread.pass }  
true  
end

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 15, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/5 "2009-01-15T13:01:52Z")

</div>

David Muir wrote:

> My current problem is that when the wxRuby’s main\_loop is invoked no  
> Ruby code gets executed. Is there any way around this? Or is this a  
> limitation of using wxRuby?

It’s a limitation of Ruby 1.8’s green threads with C-based event loops.  
There is a workaround - add something like this in your App.run block /  
App#on\_init method:

Wx::Timer.every(25) { Thread.pass }

Alternately, use Ruby 1.9 which I find does not have this problem,  
because there are real system threads underneath Ruby threads. However  
you should not update a GUI from the non-main thread.

For further, have a look at the etc/threaded.rb sample in the  
distribution, and previous discussions on this list on wxRuby + threads.

hth  
a

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 16, 2009, 1:19pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/6 "2009-01-16T13:19:39Z")

</div>

Hi guys,

I had some success yesterday so thought I should complete this thread by  
showing how I got it working.

The LogWindowDialog class is not included because it includes a lot of  
details that are irrelevant for the topic. In that class I create a  
Log4r outputter and formatter to pass LogEvent objects straight to the  
dialog which are then serialised and output into a TextCtrl widget.

Seems to work fairly well with the few examples I’ve tried. If the Ruby  
script kicks off an external app the UI will be unresponsive.

Code below. Hope it helps others.  
Cheers,  
Dave

# 

# == Description

# Generic log window dialog that integrates with our Pipeline::Log

# objects for displaying log messages to users.

# 

# === Example Usage

# # To auto-popup the display of the log dialog:

# Pipeline::GUI::LogWindow::show\_dialog( true ) do

# # Do something that will log messages, dialog only displayed on

# # errors.

# end

# 

# # To force the display of the log dialog:

# Pipeline::GUI::LogWindow::show\_dialog( ) do

# # Do something that will log messages

# end

# 

class LogWindow \< Wx::App

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

# Class Methods

#---------------------------------------------------------------------  
def initialize( log, auto\_popup, title = ‘Log’, &block )  
super( )  
@log = log  
@auto\_popup = auto\_popup  
@title = title  
@proc = Proc.new( ) do  
yield  
end  
end

# 

# Use this method to explicitly display a log dialog box. Only

# log messages generated after this call will be displayed.

# 

def LogWindow::show\_dialog( auto\_popup = false, log =  
LogSystem::instance().rootlog, title = ‘Log’ )  
begin  
dlg = LogWindow.new( log, auto\_popup, title ) do  
yield if ( block\_given? )  
end  
dlg.main\_loop( )  
rescue Exception =\> ex  
throw ex unless ( ‘exit’ == ex.message )  
end  
end

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

# Instance Methods

#---------------------------------------------------------------------  
def on\_init( )  
Wx:⏲:every( 25 ) do  
Thread::pass( )  
end  
dlg = LogWindowDialog::new( @log, @title, @auto\_popup )  
dlg.show( ) unless ( @auto\_popup )

```
script_thread = Thread.new do
  @proc.call( )
  exit( ) if ( @auto_popup and ( not dlg.is_shown ) )
end
script_thread.join( )

```

end  
end

# 

# == Description

# Log window dialog.

# 

class LogWindowDialog \< Wx::Dialog  
…  
end

---

<div class="post-metadata">

**Author:** ![dgm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/df788c/32.png) [@dgm](https://www.ruby-forum.com/u/dgm)\
**Post date:** [January 17, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/wxruby-dialog-separate-from-main-ruby-script-execution/157789/7 "2009-01-17T12:50:10Z")

</div>

David Muir wrote:

> I had some success yesterday so thought I should complete this thread by  
> showing how I got it working.

Thanks for sharing this.

> Seems to work fairly well with the few examples I’ve tried. If the Ruby  
> script kicks off an external app the UI will be unresponsive.

The trick is to call Wx::app.yield or Wx::safe\_yield (the latter in SVN  
only), while an external task is running, to tell Wx to process events  
and update the UI

alex
