Raised This Month: $ Target: $400
 0% 

CreateTimer


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PartialCloning
Senior Member
Join Date: Dec 2015
Old 02-22-2016 , 20:57   CreateTimer
Reply With Quote #1

1. How can I check if a timer of a certain ID is currently running?

2. How can I kill a timer outside the timer function?

Last edited by PartialCloning; 02-22-2016 at 20:58.
PartialCloning is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 02-22-2016 , 21:03   Re: CreateTimer
Reply With Quote #2

1. u cant 2. killtimer
Miu is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 02-22-2016 , 21:18   Re: CreateTimer
Reply With Quote #3

Can't you use

if (timer != INVALID_HANDLE)

to check if it is running?
Merudo is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-22-2016 , 21:23   Re: CreateTimer
Reply With Quote #4

Quote:
Originally Posted by Merudo View Post
Can't you use

if (timer != INVALID_HANDLE)

to check if it is running?
This only works if you've stored the handle to the timer in a variable, and the variable was correctly set to INVALID_HANDLE (or null) upon successful completion (or stopping) of the timer.

The actual data which was stored in the variable is just a number, a number that happens to correspond to the location in memory of a Sourcemod handle. It's possible (especially accidentally) to hold onto the old variable data, referencing a handle which no longer exists. AFAIK, there is no good way to verify if a handle is still valid, given only a variable that points to said handle. IsValidHandle() exists, but I am not sure of how well it functions, and using it in your plugin will cause a warning to appear upon compilation.

And in fact, the opposite is also true. It is possible to overwrite the data in such a variable, so the variable no longer points to Handle A, but instead Handle B, even while Handle A still exists. If you do not have the data referencing Handle A saved elsewhere, then the data is lost, and you may no longer have a way to remove the Handle. If this happens over and over, this is what is called "handle leaking" and an excessive amount will cause SourceMod to automatically unload your plugin.
__________________

Last edited by ddhoward; 02-22-2016 at 21:39.
ddhoward is offline
Merudo
Senior Member
Join Date: Feb 2016
Old 02-22-2016 , 21:42   Re: CreateTimer
Reply With Quote #5

Well, it's not that hard. Before creating any timer, you just do

Code:
if(timer != INVALID_HANDLE) {CloseHandle(timer); timer  = INVALID_HANDLE;}
and, when the timer actually runs, you do

Code:
timer  = INVALID_HANDLE;

Last edited by Merudo; 02-22-2016 at 21:44.
Merudo is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-22-2016 , 22:22   Re: CreateTimer
Reply With Quote #6

Quote:
Originally Posted by Merudo View Post
-snip-
Very correct, but you'd be surprised how often people forget these things.

It may also be important to put the timer = INVALID_HANDLE; part toward the TOP of the timer function. If something happens within the timer callback which causes that line to not be run (for example, a runtime error which would write to the error log), then the timer handle would become invalid, but the variable pointing to that handle would still contain the now incorrect data.

Then later, when you attempt to run KillTimer() or CloseHandle() on the handle, this will cause ANOTHER runtime error, not only causing the new timer to never be made, but all code following the timer creation within the same execution chain will also be skipped.
__________________

Last edited by ddhoward; 02-22-2016 at 22:23.
ddhoward is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 02-22-2016 , 22:42   Re: CreateTimer
Reply With Quote #7

That makes sense thanks. As for "people forget these things" that's human error that shouldn't be compensated for in the code.

I have a couple of follow up questions:

3. If I only run the timer once, do I need to close the handle or kill it or any of that?

4. Does the handle become invalid once the timer function ends if it was a non-repeating tmer?
PartialCloning is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-22-2016 , 23:00   Re: CreateTimer
Reply With Quote #8

Quote:
Originally Posted by PartialCloning View Post
3. If I only run the timer once, do I need to close the handle or kill it or any of that?

4. Does the handle become invalid once the timer function ends if it was a non-repeating tmer?
No, the handle is automatically closed if its a non-repeating timer. All you need to worry about is making sure that you also set the relevant variable to INVALID_HANDLE (or null in the new syntax.)

If you are using the new syntax, the delete keyword will BOTH run CloseHandle() AND set the variable to the appropriate value of null.

Instead of this:
Code:
CloseHandle(timer);
timer = INVALID_HANDLE;
You can use this:
Code:
delete timer;
__________________

Last edited by ddhoward; 02-22-2016 at 23:00.
ddhoward is offline
PartialCloning
Senior Member
Join Date: Dec 2015
Old 02-22-2016 , 23:42   Re: CreateTimer
Reply With Quote #9

Quote:
Originally Posted by ddhoward View Post
No, the handle is automatically closed if its a non-repeating timer. All you need to worry about is making sure that you also set the relevant variable to INVALID_HANDLE (or null in the new syntax.)

If you are using the new syntax, the delete keyword will BOTH run CloseHandle() AND set the variable to the appropriate value of null.

Instead of this:
Code:
CloseHandle(timer);
timer = INVALID_HANDLE;
You can use this:
Code:
delete timer;
5. I only need to set the variable to null if I'm doing a if(variable != null) condition or checking the variable somewhere right?

6. Using delete timer, do I need to check if the variable is not null first?
PartialCloning is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 02-23-2016 , 00:30   Re: CreateTimer
Reply With Quote #10

Quote:
Originally Posted by PartialCloning View Post
5. I only need to set the variable to null if I'm doing a if(variable != null) condition or checking the variable somewhere right?
If you're not doing something with the variable, then there's no reason to even store the returned handle into a variable at all.

Quote:
6. Using delete timer, do I need to check if the variable is not null first?
Iiiii don't know. Try it out!
__________________
ddhoward is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:04.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode