# Creating and handling a non\_GUI event

**URL:** <https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133>\
**Category:** wxRuby\
**Created:** [April 10, 2013, 7:43pm UTC](https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133 "2013-04-10T19:43:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/82dd89/32.png) [@erubin](https://www.ruby-forum.com/u/erubin)\
**Post date:** [April 10, 2013, 7:43pm UTC](https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133/1 "2013-04-10T19:43:27Z")

</div>

I need to change the value of a Wx::TextCtrl when a message is received  
in a separate thread from the GUI. I think I should be creating a class  
derived from Wx::Event (not Wx::CommandEvent) for this. But I haven’t  
been able to get it to work.

Here’s my event class:

class TextMsgEvent \< Wx::Event  
EVT\_TEXT\_MSG = Wx::EvtHandler.register\_class(self, nil,  
‘evt\_text\_msg’, 1)

def initialize(text)  
@text = text  
super(0, EVT\_TEXT\_MSG)  
end  
end

My Frame class includes this:

class myFrame \< Wx::Frame

def initialize  
…  
@text = TextCtrl.new(self, ID\_ANY, $status, Point.new(20, 20),  
Size.new(250, 150), SUNKEN\_BORDER|TE\_READONLY|TE\_MULTILINE)  
evt\_text\_msg(@text.get\_id) {|event| on\_text(event) }  
…  
end  
…  
end

When I receive the message (which contains the text) in In my non-GUI  
thread I call:

$myFrame.process\_event(TextMsgEvent.new(text))

But I don’t seem to be even getting into TextMsgEvent.initialize.

(When I was deriving from Wx::CommandEvent instead of Wx::Event, I was  
successfully creating a TextMsgEvent, but the process\_event returned  
false.)

Any suggestions? Am I even going about this in the right way?

Thanks,  
Eric R.

---

<div class="post-metadata">

**Author:** ![erubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/82dd89/32.png) [@erubin](https://www.ruby-forum.com/u/erubin)\
**Post date:** [April 10, 2013, 9:52pm UTC](https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133/2 "2013-04-10T21:52:02Z")

</div>

Hey Eric,

Do you have a GitHub repository with the actual code in it, or could you  
paste the most minimal code, that reproduces the error, so that we can  
properly diagnose the issue?

Thanks,

Mario

On Wed, Apr 10, 2013 at 1:43 PM, Eric R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> def initialize  
> thread I call:
> 
> Thanks,  
> Eric R.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> wxruby-users mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

–  
Mario S.  
Fleet Captain  
CO - Geo 99  
CO - USS T’hy’la  
XO - Diplomatic Corps - Second Life  
[http://www.iftcommand.com/chapters/thyla/](http://www.iftcommand.com/chapters/thyla/)

> **[HOME | The Federation](https://www.trekfederation.com)**

---

<div class="post-metadata">

**Author:** ![erubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/82dd89/32.png) [@erubin](https://www.ruby-forum.com/u/erubin)\
**Post date:** [April 11, 2013, 2:52pm UTC](https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133/3 "2013-04-11T14:52:58Z")

</div>

Okay, here is a minimal test program (attached).

I want my NonGuiThread to be able to change the value of the TextCtrl.  
Is add\_pending\_event the right way to do this?

(It seems that TextMsgEvent should be an Event, not a CommandEvent,  
since it’s not a GUI event, but with Event the code doesn’t work at  
all.)

Thanks,  
Eric R.

---

<div class="post-metadata">

**Author:** ![erubin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/82dd89/32.png) [@erubin](https://www.ruby-forum.com/u/erubin)\
**Post date:** [April 15, 2013, 10:09pm UTC](https://www.ruby-forum.com/t/creating-and-handling-a-non-gui-event/228133/4 "2013-04-15T22:09:19Z")

</div>

Hello Eric,

Sorry for the late reply, I was busy with Windows issues, and couldn’t  
test  
with Ruby 2.0, as wxRuby needs to be compiled with Ruby 2.0.

In any case, after testing, and throwing in a require ‘thread’ and  
Thread.abort\_on\_exception = true, I found that when you sub-class a  
Wx::Event, and attempt create a new instance of the sub-class, you will  
end  
up with an internal error, saying that no allocator has been defined for  
Wx::Event, which in all cases, is true, cause in C++ wxWidgets,  
Wx::Event,  
isn’t a True Instantiate class. In C++, the WxEvent Class is considered  
an  
Abstract Base Class, or to put it into Ruby Terms, consider WxEvent a  
Module.

It defines the base methods and instance variables / structure of the  
class  
for any sub-class of WxEvent, without actually having an allocator  
associated with the base class itself. So in order to actually get a  
Class  
that is usable in Ruby, you need to sub-class a pre-existing C++ Class,  
such as Wx::CommandEvent (WxCommandEvent class), in order to have it  
work  
in Ruby.

I hope that this helps with your future programming.

Mario

On Thu, Apr 11, 2013 at 8:52 AM, Eric R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Eric R.  
> [http://rubyforge.org/mailman/listinfo/wxruby-users](http://rubyforge.org/mailman/listinfo/wxruby-users)

–  
Mario S.  
Fleet Captain  
CO - Geo 99  
CO - USS T’hy’la  
XO - Diplomatic Corps - Second Life  
[http://www.iftcommand.com/chapters/thyla/](http://www.iftcommand.com/chapters/thyla/)

> **[HOME | The Federation](https://www.trekfederation.com)**
