Creating an App-level Timer vs. a Window level Timer

Hi Folks,

In removing a few more lurking “ThreadAccessingUIException” states, I’ve discovered two that were not showing up except in a very specific scenario - the closing of a sheet window. Since this sheet can appear attached to any of 5 windows, creating an App-level global timer to close it is what I see as the proper solution.

Since I can’t drop a Timer onto the App class, what is the best way to create a Timer and define the callback on the action?

Is this logic correct:

In App class, add a new property:

tmCloseStandby As timer

In App.Open:

tmCloseStandby = New Timer tmCloseStandby.Mode = 0 tmCloseStandby.Period = 2 AddHandler tmCloseStandby.Action, WeakAddressOf CloseWStandbySheetCB
A global method of CloseWStandbySheet containing:

Try WStandbySheet.Close Catch // should I care here? The WStandbySheet will either be open or not End Try

Then, in the other areas of the app I would call this to close the sheet window,:

App.tmCloseStandby.Mode = 1

Something’s not quite right there.

Why don’t you add the timer to the sheet window and let it close itself? If the period varies depending upon the calling window, set the period in the sheet window’s constructor.

Well, THAT gets my D’oh of the day.

You dont even need to “use” a timer if you:

xojo.core.timer.CallLater(2000, AddressOf Self.Close)

Oops - nope. Since the timer is ON the window, even setting the mode of the timer from the thread results in the TAUI exception.

[quote=372241:@JulianS]You dont even need to “use” a timer if you:

xojo.core.timer.CallLater(2000, AddressOf Self.Close)

Ah - the new Framework. However, how would that work from a call within a thread that not part of the window? Instead of Self.Close, would it be:

xojo.core.timer.CallLater(2, AddressOf WStandbySheet.Close)

Yup, that will do it

Nope - TAUI exception …

It looks like I’m going to have to do the App-level Timer so that it’s NOT on the window itself.

Okay - modification that uses Juilan’s new framework recommendation:

Add a method to my mGlobals module called g_CloseStandbySheet:

Try
    WStandbySheet.close
Catch

End Try

Then:

xojo.core.timer.CallLater(2, AddressOf g_CloseStandbySheet)

That seems to have sorted it out.

Interesting xojo.core.timer.CallLater runs on the main UI thread so it should have worked, must have been something else going on causing the issue. I’ve just dropped it into a simple test project running from a thread and it worked fine. Glad you got it sorted though :slight_smile: Yes, xojo.core.timer.CallLater is awesome!

You’re still technically accessing the window object from the thread, even if it’s just to look up where the function is in memory.