From: Karl E. Peterson on
Jim Mack submitted this idea :
> Karl E. Peterson wrote:
>> Brain freeze here, I guess.
>>
>> I need a simple one-shot timer (non form-based). I was thinking,
>> just use SetTimer, then call KillTimer in the callback. But,
>> KillTimer fails in the callback. LastDllError=0.
>>
>> What quick/easy way am I forgetting for a one-shot deal like this?
>> (Betting I need to go with mmTimers, but I've been staring at the
>> screen for too long, and need to stretch...)
>
> Of course, with an mmTimer, you can just create a one-shot and be
> done.

That's what I'm thinking too. About as clean as it gets.

--
..NET: It's About Trust!
http://vfred.mvps.org


From: Karl E. Peterson on
Bob Butler pretended :
> "Karl E. Peterson" <karl(a)exmvps.org> wrote...
>> Brain freeze here, I guess.
>>
>> I need a simple one-shot timer (non form-based). I was thinking, just use
>> SetTimer, then call KillTimer in the callback. But, KillTimer fails in the
>> callback. LastDllError=0.
>
> works for me; what makes you think it is failing? KillTimer return value 0
> is error, non-zero is success

Retval is 0. Timer keeps firing.

> Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
> ByVal nIDEvent As Long) As Long
> Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, _
> ByVal nIDEvent As Long, ByVal uElapse As Long, _
> ByVal lpTimerFunc As Long) As Long
>
> Public Sub StartTimer(ByVal hwnd As Long)
> Dim x As Long
> x = SetTimer(hwnd, 1, 2000, AddressOf TimerFunc)
> Debug.Print "Start", x, Err.LastDllError
> End Sub
>
> Private Sub TimerFunc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent
> As Long, ByVal dwTime As Long)
> Dim x As Long
> Static k As Long
> k = k + 1
> If k = 5 Then
> x = KillTimer(hwnd, idEvent)
> Debug.Print "Stop", x, Err.LastDllError
> Else
> Debug.Print "Fire"
> End If
> End Sub

Hmmmm, that gives me a thought...

--
..NET: It's About Trust!
http://vfred.mvps.org


From: Nobody on
"Karl E. Peterson" <karl(a)exmvps.org> wrote in message
news:i0gar1$ooe$1(a)news.eternal-september.org...
> Brain freeze here, I guess.
>
> I need a simple one-shot timer (non form-based). I was thinking, just use
> SetTimer, then call KillTimer in the callback. But, KillTimer fails in
> the callback. LastDllError=0.
>
> What quick/easy way am I forgetting for a one-shot deal like this?
> (Betting I need to go with mmTimers, but I've been staring at the screen
> for too long, and need to stretch...)

You probably didn't use the return value of SetTimer, but I could be
mistaken. Here is an example to try. Post the code in Module1, and remove
Form1 from a new project.


Option Explicit

Public Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long, ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long
Public Const WM_TIMER = &H113
Public TimerID As Long
Public Done As Boolean

Public Sub Main()

Done = False
TimerID = SetTimer(0, 0, 5000, AddressOf TimerProc)
Debug.Print "SetTimer returned " & TimerID & ", LastDllError = " & _
Err.LastDllError
Do While Not Done
DoEvents
Loop
End Sub

Public Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, _
ByVal idEvent As Long, ByVal dwTime As Long)
Dim ret As Long

Debug.Print "Timer"
ret = KillTimer(0, TimerID)
Debug.Print "KillTimer returned " & ret & ", LastDllError = " & _
Err.LastDllError
Done = True
End Sub



Output(In the IDE using VB6+SP5 on XP+SP2):

SetTimer returned 22353, LastDllError = 0
Timer
KillTimer returned 1, LastDllError = 0



From: Jim Mack on
Karl E. Peterson wrote:
> Jim Mack submitted this idea :
>> Karl E. Peterson wrote:
>>> Brain freeze here, I guess.
>>>
>>> I need a simple one-shot timer (non form-based). I was thinking,
>>> just use SetTimer, then call KillTimer in the callback. But,
>>> KillTimer fails in the callback. LastDllError=0.
>>>
>>> What quick/easy way am I forgetting for a one-shot deal like this?
>>> (Betting I need to go with mmTimers, but I've been staring at the
>>> screen for too long, and need to stretch...)
>>
>> Of course, with an mmTimer, you can just create a one-shot and be
>> done.
>
> That's what I'm thinking too. About as clean as it gets.

Clean except you can't do any real work in the callback, so you end up
complicating it again with a flag or a message...

--
Jim

From: Karl E. Peterson on
Jim Mack laid this down on his screen :
> Karl E. Peterson wrote:
>> Jim Mack submitted this idea :
>>> Karl E. Peterson wrote:
>>>> Brain freeze here, I guess.
>>>>
>>>> I need a simple one-shot timer (non form-based). I was thinking,
>>>> just use SetTimer, then call KillTimer in the callback. But,
>>>> KillTimer fails in the callback. LastDllError=0.
>>>>
>>>> What quick/easy way am I forgetting for a one-shot deal like this?
>>>> (Betting I need to go with mmTimers, but I've been staring at the
>>>> screen for too long, and need to stretch...)
>>>
>>> Of course, with an mmTimer, you can just create a one-shot and be
>>> done.
>>
>> That's what I'm thinking too. About as clean as it gets.
>
> Clean except you can't do any real work in the callback, so you end up
> complicating it again with a flag or a message...

True. Solved it, though, at Bob's instigation. Remember I said it was
already pretty complex? D'oh! Had the wrong hWnd in the kill call.
Time for nice *long* break... :-S

--
..NET: It's About Trust!
http://vfred.mvps.org