Wintimer Problem

Jaye7

Well-known Member
Joined
Jul 7, 2010
Messages
1,069
I have a wintimer, however occasionally it has a problem where even though I cancel the timer it just keeps running anyway and the only way I have found to stop it is to close the file and re-open it otherwise the timer will not function properly as it is like now there are two timers running which confuse each other, does anyone have a suggestion on how to fix this problem without having to close my workbook.

The script I use to end the timer follows.

Code:
Sub EndTimer()
    'On Error Resume Next
    KillTimer 0&, TimerID
end sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You need to make sure that the TimerID variable is seen by EndTimer sub by declaring it as a module level or Public variable.Also, the project must not lose state or be reset as the timer handle stored in the TimerID variable will then be lost .

A safer approach that I tend to use is to associate the timers with the hwnd of a window within the current Process. Doing this will ensure that the Timers don't rely on variables that can be accidently lost and will ensure that the timers are always properly terminated.

Try this example :

Code:
Option Explicit

Private Declare Function SetTimer Lib "user32" _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
 
Private Declare Function KillTimer Lib "user32" ( _
ByVal hWnd As Long, _
ByVal uIDEvent As Long) As Long
 

Private Declare Function FindWindowEx Lib "user32" _
Alias "FindWindowExA" _
(ByVal hwnd1 As Long, _
ByVal hwnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long



Sub StartTimers()

    'Timer 1
     Call SetTimer(Application.hWnd, 0, 1000, AddressOf TimerProc1)
     
    'Timer 2
    Call SetTimer((FindWindowEx(Application.hWnd, 0, "XLDESK", vbNullString)), _
     0, 2000, AddressOf TimerProc2)
 

End Sub

Sub StopTimers()

    KillTimer Application.hWnd, 0
    KillTimer (FindWindowEx(Application.hWnd, 0, "XLDESK", vbNullString)), 0
    Debug.Print "Timers stopped."
    
End Sub


Private Function TimerProc1( _
    ByVal hWnd As Long, _
    ByVal uMsg As Long, _
    ByVal idEvent As Long, _
    ByVal dwTime As Long _
)
     Debug.Print "Timer1 running: " & Format(Now, "hh:mm:ss")

End Function

Private Function TimerProc2( _
    ByVal hWnd As Long, _
    ByVal uMsg As Long, _
    ByVal idEvent As Long, _
    ByVal dwTime As Long _
)
     Debug.Print "Timer2 running: " & Format(Now, "hh:mm:ss")

End Function
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top