MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / UpdateOrCreateTimer

Method UpdateOrCreateTimer

source/script.cpp:4042–4116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4040
4041
4042ResultType Script::UpdateOrCreateTimer(IObject *aCallback
4043 , bool aUpdatePeriod, __int64 aPeriod, bool aUpdatePriority, int aPriority)
4044// Caller should specific a blank aPeriod to prevent the timer's period from being changed
4045// (i.e. if caller just wants to turn on or off an existing timer). But if it does this
4046// for a non-existent timer, that timer will be created with the default period as specified in
4047// the constructor.
4048{
4049 ScriptTimer *timer;
4050 for (timer = mFirstTimer; timer != NULL; timer = timer->mNextTimer)
4051 if (timer->mCallback == aCallback) // Match found.
4052 break;
4053 bool timer_existed = (timer != NULL);
4054 if (!timer_existed) // Create it.
4055 {
4056 if ( !(timer = new ScriptTimer(aCallback)) )
4057 return MemoryError();
4058 if (!mFirstTimer)
4059 mFirstTimer = mLastTimer = timer;
4060 else
4061 {
4062 mLastTimer->mNextTimer = timer;
4063 // This must be done after the above:
4064 mLastTimer = timer;
4065 }
4066 ++mTimerCount;
4067 }
4068
4069 if (!timer->mEnabled)
4070 {
4071 timer->mEnabled = true;
4072 ++mTimerEnabledCount;
4073 SET_MAIN_TIMER // Ensure the API timer is always running when there is at least one enabled timed subroutine.
4074 if (timer_existed)
4075 {
4076 // mEnabled is currently used to mark a running timer for deletion upon return.
4077 // Since the timer could be recreated by a different thread which just happens
4078 // to interrupt the timer thread, it seems best for consistency to reset Period
4079 // and Priority as though it had already been deleted.
4080 aUpdatePeriod = true;
4081 aUpdatePriority = true;
4082 }
4083 }
4084
4085 if (aUpdatePeriod)
4086 {
4087 if (aPeriod < 0) // Support negative periods to mean "run only once".
4088 {
4089 timer->mRunOnlyOnce = true;
4090 timer->mPeriod = (DWORD)-aPeriod;
4091 }
4092 else // Positive number.
4093 {
4094 timer->mPeriod = (DWORD)aPeriod;
4095 timer->mRunOnlyOnce = false;
4096 }
4097 }
4098
4099 if (aUpdatePriority)

Callers 1

BIF_DECLFunction · 0.80

Calls 1

MemoryErrorFunction · 0.85

Tested by

no test coverage detected