Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Runtime.InteropServices
- Public Class MillisecondTimer
- Public Event Elapsed()
- Private mTimerId As Integer = 0
- Private mHandler As TimerEventDel
- Private _Interval As Integer = 0
- Private Delegate Sub TimerEventDel(ByVal id As Integer, _
- ByVal msg As Integer, _
- ByVal user As IntPtr, _
- ByVal dw1 As Integer, _
- ByVal dw2 As Integer)
- <DllImport("winmm.dll")> _
- Private Shared Function timeBeginPeriod(ByVal msec As Integer) As Integer
- End Function
- <DllImport("winmm.dll")> _
- Private Shared Function timeEndPeriod(ByVal msec As Integer) As Integer
- End Function
- <DllImport("winmm.dll")> _
- Private Shared Function timeSetEvent(ByVal delay As Integer, ByVal resolution As Integer, _
- ByVal handler As TimerEventDel, ByVal user As IntPtr, _
- ByVal eventType As Integer) As Integer
- End Function
- <DllImport("winmm.dll")> _
- Private Shared Function timeKillEvent(ByVal id As Integer) As Integer
- End Function
- Public Property Interval() As Integer
- Get
- Return _Interval
- End Get
- Set(value As Integer)
- If mTimerId <> 0 Then [Stop]()
- _Interval = value
- Start()
- End Set
- End Property
- Public Sub New()
- timeBeginPeriod(1)
- mHandler = New TimerEventDel(AddressOf TimerCallback)
- End Sub
- Public Sub New(ByVal Milliseconds As Integer)
- timeBeginPeriod(1)
- mHandler = New TimerEventDel(AddressOf TimerCallback)
- _Interval = Milliseconds
- End Sub
- Public Sub Start()
- If mTimerId = 0 Then mTimerId = timeSetEvent(_Interval, 0, mHandler, IntPtr.Zero, 1)
- End Sub
- Public Sub [Stop]()
- timeKillEvent(mTimerId)
- mTimerId = 0
- timeEndPeriod(1)
- End Sub
- Private Sub TimerCallback(ByVal id As Integer, ByVal msg As Integer, _
- ByVal user As IntPtr, ByVal dw1 As Integer, ByVal dw2 As Integer)
- If mTimerId <> 0 Then RaiseEvent Elapsed()
- End Sub
- Protected Overrides Sub Finalize()
- timeKillEvent(mTimerId)
- MyBase.Finalize()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment