Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SetTimer allways crashes 1

Status
Not open for further replies.

harmmeijer

Programmer
Mar 1, 2001
869
CN
I have a standart exe vb project sub main starts it up
This is the 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

Sub main()
Dim bs As Long
SetTimer bs, bs, 200, AddressOf mysub
End Sub

Sub mysub()
Debug.Print Now()
End Sub

After pressing F5 Now() is printed one time and then Dr. Watson takes over got the following from the application log:
The application, EXE\VB6.dbg, generated an application error The error occurred on 3/20/2002 @ 11:39: 9.391 The exception generated was c000001d at address 0012fe9d (<nosymbols>)

I am using win NT workstation versoin 4.00.1381.
Rebooted several times and downloaded some example code wich basically uses the same code (only with window handle HOW DO YOU USE THAT IN A DLL) but these crash in the same way.
 
I am no API expert but this line

SetTimer bs, bs, 200, AddressOf mysub

should have two seperate variables like this

SetTimer bs, sb, 200, AddressOf mysub
Anything is possible, the problem is I only have one lifetime.[thumbsup2]
 
I do not use the window handle so the bs is only there to supply a null value.
 
You cannot just use any old subroutine's address. The callback function has to follow a particular template, in this case TimerProc, which looks as follows:
[tt]
Public Sub TimerProc (ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
[/tt]
As long as your procedure uses this template, you can use it as the destination for the callback. So your code would become:
[tt]
Option Explicit

Private Declare Function SetTimer Lib &quot;user32&quot; _
(ByVal hWnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Sub main()
Dim bs As Long
SetTimer bs, bs, 200, AddressOf mysub
End Sub

Public Sub mysub(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Debug.Print Now()
End Sub
[/tt]
Be warned that there are a couple of other issues with your code - the main one being that you have not made any provision for killing the timer after you've finished using it...

 
Perfect, it works.
Thanks for you time strongm, it was quite confusing that the code worked in win 2000. Below is the code with a killtimer:

in the module:
Option Explicit

Private Declare Function SetTimer Lib &quot;user32&quot; _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long
Public Declare Function KillTimer Lib &quot;user32&quot; (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Dim lngTimerID As Long
Public intCounter As Integer

Public Sub main()
lngTimerID = SetTimer(0, 0, 200, AddressOf mysub)
End Sub

Public Sub mysub(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
Form1.Label1.Caption = Now()
intCounter = intCounter + 1
If intCounter > 9 Then
KillTimer 0, lngTimerID
End If
End Sub

in form1 containing label1:
Private Sub Form_Load()
main
End Sub

Private Sub Form_Unload(Cancel As Integer)
Module1.intCounter = 100
Module1.mysub 0, 0, 0, 0
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top