# Preloading the wx libraries

**URL:** <https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140>\
**Category:** wxRuby\
**Created:** [July 15, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140 "2008-07-15T17:14:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Magnus\_EngstrSSSSm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Magnus\_EngstrSSSSm](https://www.ruby-forum.com/u/Magnus_EngstrSSSSm)\
**Post date:** [July 15, 2008, 5:14pm UTC](https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140/1 "2008-07-15T17:14:38Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi,

I have a Nokia N810, running Linux, and wxruby works quite well on it,  
except application startup which takes quite some time.

I have thought about preloading the wx libraries in some kind of  
launcher, which then brings up the forms on some signal.

I tried calling MyApplication.new.main\_loop multiple times (exited the  
main loop in between), which complained about the constant THE\_APP  
already being defined. Undefining this and trying again gave me a  
segfault. I also tried handling this in the on\_init method, but it  
didn’t work so well when it wasn’t allowed to return 😉

Do someone have any suggestions about how to create this type of  
launcher?

Best regards,  
Magnus  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkh8uS0ACgkQFsdVGDM22G6KigCbB57hPWZY+UFdnPfqIhCxsu3J  
VlYAn1sxXgSydAnmywJ8lUQAWtdHttKr  
=BLZH  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![Magnus\_EngstrSSSSm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Magnus\_EngstrSSSSm](https://www.ruby-forum.com/u/Magnus_EngstrSSSSm)\
**Post date:** [July 15, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140/2 "2008-07-15T19:28:22Z")

</div>

_mouth drops_ I never knew wxRuby was being used, or could even be used  
on  
something like that little thing. The problem with that, is you need to  
initialize Wx::App only once. Any attempt to use Wx::App more then  
once,  
will cause the error your getting. You could possibly setup a  
Client/Server  
system, in which you create a dummy library, that replicates the  
functionality of wxRuby, which actually communicates to the Server, to  
create the windows, and such. That would be the only way you would be  
able  
to “Pre Load” the wxRuby library.

Sorry, that’s about as much as I can help you with.

L8ers,

---

<div class="post-metadata">

**Author:** ![Magnus\_EngstrSSSSm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Magnus\_EngstrSSSSm](https://www.ruby-forum.com/u/Magnus_EngstrSSSSm)\
**Post date:** [July 16, 2008, 1:08am UTC](https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140/3 "2008-07-16T01:08:00Z")

</div>

Hi

Mario S. wrote:

> _mouth drops_ I never knew wxRuby was being used, or could even be  
> used on something like that little thing.  
> Likewise … cool

> The problem with that, is you need to initialize Wx::App only once.  
> Any attempt to use Wx::App more then once, will cause the error your  
> getting.  
> Generally, this is part of the design of the Wx library, not just wxRuby

- it expects the main\_loop to be entered only once, as part of the  
library is initialised at this point rather than at library load.

> You could possibly setup a Client/Server system, in which you create a  
> dummy library, that replicates the functionality of wxRuby, which  
> actually communicates to the Server, to create the windows, and such.  
> That would be the only way you would be able to “Pre Load” the wxRuby  
> library.  
> I looked into this approach a while back when I was trying to create an  
> interactive wxRuby shell which could be used to test out ideas quickly.  
> I never got it into a fully functional state, but here’s the code in  
> case it helps you. It creates a server app upon which a client can  
> execute wx code.

alex

##### CLIENT

require ‘socket’  
include Socket::Constants

while command = gets  
if command.chomp == “x”

```
exit!

```

end  
p ‘command’  
socket = Socket.new( AF\_INET, SOCK\_STREAM, 0 )  
sockaddr = Socket.pack\_sockaddr\_in( 5858, ‘127.0.0.1’ )  
socket.connect( sockaddr )  
socket.write( command )  
result = socket.readline  
STDOUT.puts “\>\> #{result}”  
socket.close  
end

# results = socket.read

##### SERVER

require ‘wx’

require ‘socket’

class IRBApp \< Wx::App  
def on\_init

```
server = TCPServer.new('127.0.0.1', 5858)

$f = Wx::Frame.new(nil, -1, 'empty')
# $f.show
Wx::Timer.every(100) { Thread.pass }

listener = Thread.new do
  while session = server.accept
    command = session.gets
    p command
    begin
      result = eval( command )
    rescue => result
    end
    session.puts(result.inspect)
    session.close
  end
end
listener.abort_on_exception = true

```

end  
end

IRBApp.new().main\_loop

---

<div class="post-metadata">

**Author:** ![Magnus\_EngstrSSSSm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@Magnus\_EngstrSSSSm](https://www.ruby-forum.com/u/Magnus_EngstrSSSSm)\
**Post date:** [July 16, 2008, 8:57am UTC](https://www.ruby-forum.com/t/preloading-the-wx-libraries/143140/4 "2008-07-16T08:57:45Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

I tried this approach, but I missed the Thread.pass bit, so my threads  
wasn’t very effective when I wasn’t operating on the frames 🙂

I’ll play around a bit and see what happens.  
Thanks!

/Magnus

Alex F. wrote:

> Generally, this is part of the design of the Wx library, not just wxRuby  
> case it helps you. It creates a server app upon which a client can  
> while command = gets  
> socket.close  
> def on\_init  
> p command  
> end
> 
> IRBApp.new().main\_loop
> 
> * * *
> 
> 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)

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkh9m3gACgkQFsdVGDM22G6jzACdGkC6BEYamPeiQYTgD3o/61F/  
BuAAn0X1kjRQGFoKdAkkespIylDu+Tsc  
=d6R+  
-----END PGP SIGNATURE-----
