# Application focus

**URL:** <https://www.ruby-forum.com/t/application-focus/187820>\
**Category:** wxRuby\
**Created:** [April 29, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/application-focus/187820 "2010-04-29T22:54:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![freaxmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@freaxmind](https://www.ruby-forum.com/u/freaxmind)\
**Post date:** [April 29, 2010, 10:54pm UTC](https://www.ruby-forum.com/t/application-focus/187820/1 "2010-04-29T22:54:22Z")

</div>

Hi there,

I’m developping an application based on xmpp4r and WxRuby to download  
file between contacts. My application must handle incoming request and  
manage all the file upload proccess.

Unfortunately, all proccess in Wx::App are active only if the  
application has the focus or receive an event.  
For example, I must move my cursor on the main window or click on it if  
I want the user interface to be refreshed (contact go online/offline).

Is there a way to set that the Wx application must continue its actions  
allthough it has not the focus ?

Thank you

---

<div class="post-metadata">

**Author:** ![freaxmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@freaxmind](https://www.ruby-forum.com/u/freaxmind)\
**Post date:** [April 29, 2010, 11:27pm UTC](https://www.ruby-forum.com/t/application-focus/187820/2 "2010-04-29T23:27:19Z")

</div>

Hello Hurier,

There is two ways in which you can do this. The first method, is to use  
a  
Wx::Timer and Threads. It depends on if you are using Ruby 1.9 or Ruby  
1.8  
in the method in which you can execute this.

If you are using Ruby 1.8

You create a Thread that will do your Xmppr processing, and then you  
setup a  
timer, that will after so many seconds or Milliseconds, does a Thread  
switch, to allow other ruby threads to run. You can find an example of  
this  
in my Sockets examples distributed with wxRuby.

If you are using Ruby 1.9

You can simply just create the Thread, and it will auto-context switch  
for  
you, as Ruby 1.9 uses native threads, instead of Soft Threads, as with  
Ruby  
1.8, hence for the work around provided in the sockets demo.

The second method in which to implement this, is to use the evt\_idle  
method  
in your initialization method for your Window class, which will call the  
method that handles the Xmppr processing, and occasionally checks for  
Events  
to be generated by the wxWidgets, by using Wx::App.pending? Which  
returns  
true if events are pending, otherwise false.

Idle events are generated once for entering an Idle state. So, if you  
do  
nothing with your app for a second, it will generate an idle event,  
evt\_idle  
will execute once, and then it will wait for new events to occur. When  
a  
new event occurs for anything, the event will fire, then it will wait  
for a  
second then generate another idle event.

The timing in the above explination is only an example, and does not  
expressly garuntee that the idle event will execute 1 second after no  
other  
events occur.

hth,

Mario

---

<div class="post-metadata">

**Author:** ![freaxmind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f475e1/32.png) [@freaxmind](https://www.ruby-forum.com/u/freaxmind)\
**Post date:** [May 3, 2010, 10:09pm UTC](https://www.ruby-forum.com/t/application-focus/187820/3 "2010-05-03T22:09:03Z")

</div>

Thanks Mario, it’s exacly that’s I need ! I’ve used the Timer class and  
set it in my main windows. It’s work perfectly !

Thanks again for your accurate answer.
