Discussion:
Exit application using timer causes timer "cannot clear because it is in use"
(too old to reply)
Terry Syverson
2010-12-23 19:30:33 UTC
Permalink
Hi - I am stumped on an issue with a VFP app I am working on - I am
trying to close the application at midnight (I use sys(2) subtracted
from 86,400 to get the time until midnight and set the timer interval
to that value * 1000).
The problem I am having is that when I issue the "clear all" command,
I get the error "Cannot clear object oTimer because it is still in
use"... Here is the code I'm using:

_screen.AddObject('oTimer','MyTimer')

DEFINE CLASS MyTimer as Timer

* Interval is in milliseconds.
* To get 45 minutes... 60 seconds * 45 * 1000 = 2700000
* To get 5 minutes - 60 * 5 * 1000 = 300000
interval = nMSUntilMidnight

procedure Timer

CLEAR EVENTS
_screen.RemoveObject('oTimer')
CLOSE ALL
CLEAR ALL
RELEASE ALL
QUIT

ENDPROC


For some reason, the RemoveObject doesn't appear to be working,
because I continue to get the error - has anyone ever encountered this
before?

Thanks!
-Terry
Dan Freeman
2010-12-23 21:29:49 UTC
Permalink
Couple of thoughts:

1) QUIT is ABSOLUTELY NEVER required in a runtime application. When the
code ends, the application ends.

2) _SCREEN isn't particularly cooperative about "contained" objects,
but you can store an object in a property of _SCREEN:

With _Screen
.AddProperty("MyTimer")
.MyTimer = CreateObject(....)
Endwith

3) In your timer method, your first step should be to disable the timer
itself:

this.Enabled = .f.

4) An object can't remove itself. But it can call its own .Release()
method.

As an aside, timer events were meant to be fired at regular intervals.
Inside that method, measure the time and see if you're after midnight.
You're swimming upstream a little bit to set it to fire only once, at
an exact time.

Dan
Post by Terry Syverson
Hi - I am stumped on an issue with a VFP app I am working on - I am
trying to close the application at midnight (I use sys(2) subtracted
from 86,400 to get the time until midnight and set the timer interval
to that value * 1000).
The problem I am having is that when I issue the "clear all" command,
I get the error "Cannot clear object oTimer because it is still in
_screen.AddObject('oTimer','MyTimer')
DEFINE CLASS MyTimer as Timer
* Interval is in milliseconds.
* To get 45 minutes... 60 seconds * 45 * 1000 = 2700000
* To get 5 minutes - 60 * 5 * 1000 = 300000
interval = nMSUntilMidnight
procedure Timer
CLEAR EVENTS
_screen.RemoveObject('oTimer')
CLOSE ALL
CLEAR ALL
RELEASE ALL
QUIT
ENDPROC
For some reason, the RemoveObject doesn't appear to be working,
because I continue to get the error - has anyone ever encountered this
before?
Thanks!
-Terry
Zootal
2010-12-30 23:01:47 UTC
Permalink
Post by Terry Syverson
Hi - I am stumped on an issue with a VFP app I am working on - I am
trying to close the application at midnight (I use sys(2) subtracted
from 86,400 to get the time until midnight and set the timer interval
to that value * 1000).
The problem I am having is that when I issue the "clear all" command,
I get the error "Cannot clear object oTimer because it is still in
_screen.AddObject('oTimer','MyTimer')
DEFINE CLASS MyTimer as Timer
* Interval is in milliseconds.
* To get 45 minutes... 60 seconds * 45 * 1000 = 2700000
* To get 5 minutes - 60 * 5 * 1000 = 300000
interval = nMSUntilMidnight
procedure Timer
CLEAR EVENTS
_screen.RemoveObject('oTimer')
CLOSE ALL
CLEAR ALL
RELEASE ALL
QUIT
ENDPROC
For some reason, the RemoveObject doesn't appear to be working,
because I continue to get the error - has anyone ever encountered this
before?
Thanks!
-Terry
You can't remove the timer from code the timer is running. You need to
remove the timer object from code running outside of the timer.

This applies to pretty much anything - you can't delete an object from
within the object. To do what you want, set the enabled property of the
timer to false, and enable another timer with an interval sufficient for
the first timer to terminate. Once the first timer is done executing the
code, the second timer fires and the other timer will quite happily
remove the first. Then you have the problem of the second timer hanging
out there.

I think a far better solution would be to just have a permanent timer
that is always there, and enable it when you need to.

And yes, NEVER hand objects off of _screen. I know a lot of people like
to do it, but I've always considered it to be a bad idea. Just create a
public object, and hang your timers and stuff off of that (which I'm sure
others way is an equally bad idea).

Better yet, just make the timer existence permanent

Loading...