Hoi,
Als ik (bijvoorbeeld) een timer nodig heb in een programma, gebruik ik altijd een aparte thread (
http://msdn.microsoft.com/en-us/library/ms171728.aspx), waarin ik dan kijk wat het verschil is tussen de starttijd van de thread (opgeslagen in een variable) en de huidige tijd (
http://msdn.microsoft.com/en-us/library/system.datetime.now.aspx).
Vervolgens is het wel handig de timer steeds even stil te zetten zodat hij niet constant aan staat, wat met de volgende code kan:
[CPP]System.Threading.Thread.Sleep(milliseconds)[/CPP]
Vb: (geeft de tijd weer in een label)
[CPP]Imports System.Threading
Public Class Form1
Private ThrTijd As New Thread(AddressOf Tijd)
Private Delegate Sub DelLabelText(ByVal Text As String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ThrTijd = New Thread(AddressOf Tijd)
ThrTijd.Start()
End Sub
Private Sub Tijd()
Do
LabelText(Now.Hour & ":" & Now.Minute & ":" & Now.Second & "." & Now.Millisecond)
Loop
End Sub
Private Sub LabelText(ByVal Text As String)
If InvokeRequired Then
Dim d As New DelLabelText(AddressOf LabelText)
Me.Invoke(d, New Object() {Text})
Else
Label1.Text = Text
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If ThrTijd.IsAlive Then
ThrTijd.Abort()
End If
End Sub
End Class
[/CPP]
Succes!
MartinJM