Window loses focus, timer stops?

Hi All.

I was wondering about a timer in a window stopping if ANOTHER type of window (safari, etc) got focus. Doing some searching I found this thread and have a question:

Is this just for windows in the desktop app you created, or is it relevant to when other types of windows, as suggested above get the focus?


You could instantiate your timers in code. Presumably you dragged them to a window from the Library? I don’t have any timers “attached to a window”, and they all seem to run as expected.

Hi Tim.

Yes I dragged them from the Library, and not in code. My program runs fine, but I was curious having run across the thread I attached, while looking at something else.

I’m a curious humanoid… so figured I would ask.


You didn’t specify which platform(s) you see this on. But if you are talking about macOS, you may be the victim of a “feature” called “app nap” where Apple tries to minimize resource usage (particularly battery, cpu, or active memory) and automagically put a GUI app to sleep while the window is not visible – i.e. minimized or fully covered by other applications.

And a long time ago, Apple exposed an easy way in the macOS SDK to disable that but later macOS SDK versions do not offer that anymore. You may find parts of this thread educational for ways to still disable it from your code.

Sorry for taking so long to get back. Busy busy.
Since in Catalina there is no “disable appnap” checkbox in “Get Info”, I opened Activity Monitor, and added the column AppNap. It shows whether an app sleeps without being the focused window. I had appnap = No.

I verified that my app doesn’t sleep no matter which of 100 windows I have open.


There is a way with API to tell the macOS NOT to nap your application if it busy. I’ll have to look it up if you are interested.

Instead of placing the timer on a window, which indeed stops execution when it is in the background, use a timer in App.

  1. Add a property as timer
    In App.Open:
  2. Use addhandler to add a timer method to handle timer action.

Oddly, I spent a few hours trying to make exactly this work yesterday.
Complete failure… no variation on the addhandler line would compile for me.
I added an invisible window to do the job, and it seems to work
This post suggests that sooner or later, it wont, as the window isnt visible.

Anyone have a working code example of a timer in code , that works under Xojo 2018 R3 (which Im using for my legacy projects)

Here’s what I do, in a method called from App.Open:

app.generalTimer         = new Timer
app.generalTimer.enabled = True
app.generalTimer.RunMode = Timer.RunModes.Multiple         // Ticks once per minute
app.generalTimer.period  = 60000                           // for the life of the app

AddHandler app.generalTimer.Action, WeakAddressOf app.generalTimerEvent


Same error:

Type Mismatch error
Expected delegate Delegate(Timer) but got delegate Delegate()

What exactly is app.generalTimerEvent?
In my code it is a method with no return value

Does your method generalTimerEvent() have a proper signature like:

Function generalTimerEvent(sender As Timer)

If macOS is pausing your app in the background, you can use beginActivity in NSProcessInfoMBS class to let the system know what you do.

Thats the trick.
Quite obscure in the Xojo documentation…
In Xojo 2018 it turns out to be .mode rather than .runmode , with different enumeration too.
(Another one of those seemingly pointless language changes)

Working now though, thanks!

1 Like

It’s a method where I do stuff because the timer went off.

Ah, what you meant to ask was “What is the definition of app.generalTimerEvent?”

It needs to have (as in my case):

tea as timer

as a parameter.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.