Raised This Month: $ Target: $400
 0% 

Regarding KillTimer()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 09-12-2011 , 08:37   Regarding KillTimer()
Reply With Quote #1

I started scripting for SM a few days ago and I have a question about timers.

When I perform a console command, timer is created. If I perform the command again while the timer is still on its way, the timer is killed so I could create a new one (different one). I managed to get it working using simple logic, however it seems a little strange to me for some reason.
Code:
new Handle:g_hTimer = INVALID_HANDLE; public OnPluginStart()     RegConsoleCmd("sm_timertest", OnTimerTest); public Action:OnTimerTest(iArgs) {     PrintToServer("Command called.");     // Command is called. Checking if it's been called not long ago     if (g_hTimer != INVALID_HANDLE)     {         // Yes it was, let's kill the timer         KillTimer(g_hTimer);         // Or this?         // CloseHandle(g_hTimer);     }     static s_iTestData;     g_hTimer = CreateTimer(3.0, OnTimer, s_iTestData++); } public Action:OnTimer(Handle:hTimer, any:iData) {     PrintToServer("Timer called.");     // Am I supposed to do this?     // If it's not done then when I call the command, it will try to kill an invalid timer     g_hTimer = INVALID_HANDLE; }
In AMXX it was simply
Code:
remove_task(iTask);
which automatically checked if such task exists, not requiring any extra work, that's why I'm asking if I'm required to do all this in SM.
__________________
hleV is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 09-12-2011 , 09:05   Re: Regarding KillTimer()
Reply With Quote #2

PHP Code:
    // Command is called. Checking if it's been called not long ago
    
if (g_hTimer != INVALID_HANDLE)
    {
        
// Yes it was, let's kill the timer
        
KillTimer(g_hTimer);

        
// Clear handle
        
g_hTimer INVALID_HANDLE

    

*edti
Timers - Timed callbacks.
__________________
Do not Private Message @me
Bacardi is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 09-12-2011 , 09:37   Re: Regarding KillTimer()
Reply With Quote #3

Quote:
Originally Posted by Bacardi View Post
PHP Code:
        // Clear handle
        
g_hTimer INVALID_HANDLE

    

That's unnecessary since I'm assigning g_hTimer to a new CreateTimer() value in the very same function.

Anyway, thanks. It seems that I've been doing it the right way.
__________________
hleV is offline
Antithasys
Moderator
Join Date: Apr 2008
Old 09-12-2011 , 20:53   Re: Regarding KillTimer()
Reply With Quote #4

While you are correct in this very specific case... It is good practice to reset any global handle variables or arrays back to INVALID_HANDLE once the handle is closed... (In this case, KillTimer and CloseHandle are virtually the same)

I suggest the following stock for dealing with timers...

PHP Code:
stock ClearTimer(&Handle:timer

    if (
timer != INVALID_HANDLE
    { 
        
KillTimer(timer); 
    } 
    
timer INVALID_HANDLE

Using this stock, ClearTimer(g_hTimer); will always take care of your active timer and variable assignment.
__________________
[my plugins]

When you think about asking a question... consider what have you tried?
Antithasys is offline
TnTSCS
AlliedModders Donor
Join Date: Oct 2010
Location: Undisclosed...
Old 11-14-2011 , 18:24   Re: Regarding KillTimer()
Reply With Quote #5

Nice stock Thank you
TnTSCS 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 21:06.


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