Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class GameTimer
- <Runtime.InteropServices.DllImport("winmm.dll")>
- Private Shared Function timeBeginPeriod(ByVal msec As UInteger) As UInteger : End Function
- <Runtime.InteropServices.DllImport("winmm.dll")>
- Private Shared Function timeEndPeriod(ByVal msec As UInteger) As UInteger : End Function
- <Runtime.InteropServices.DllImport("winmm.dll")>
- Private Shared Function timeSetEvent(ByVal delay As UInteger, ByVal resolution As UInteger, ByVal handler As TimerEventDel, ByVal user As IntPtr, ByVal eventType As UInteger) As UInteger : End Function
- <Runtime.InteropServices.DllImport("winmm.dll")>
- Private Shared Function timeKillEvent(ByVal id As UInteger) As UInteger : End Function
- Private Delegate Sub TimerEventDel(ByVal id As UInteger, ByVal msg As UInteger, ByVal user As IntPtr, ByVal dw1 As IntPtr, ByVal dw2 As IntPtr)
- Public Shared Event Elapsed()
- Private Shared ReadOnly TicksPerMillisecond As Decimal = Stopwatch.Frequency / 1000
- Private Shared ReadOnly MicroWait As New Threading.AutoResetEvent(False)
- Private Shared NextTime As Decimal = Long.MaxValue
- Private Shared Interval As Decimal = Stopwatch.Frequency
- Private Shared EventThread As New Threading.Thread(Sub()
- While True
- Dim Ms As Integer = Math.Round(((NextTime - Stopwatch.GetTimestamp) / TicksPerMillisecond) - 1)
- If Ms > 1 Then
- timeSetEvent(Ms, 0, New TimerEventDel(Sub(ByVal id As UInteger, ByVal msg As UInteger, ByVal user As IntPtr, ByVal dw1 As IntPtr, ByVal dw2 As IntPtr)
- MicroWait.Set()
- While timeKillEvent(id) <> 0UI : Threading.Thread.Sleep(1) : End While
- End Sub), IntPtr.Zero, 0)
- MicroWait.WaitOne()
- End If
- While Stopwatch.GetTimestamp < NextTime : End While
- RaiseEvent Elapsed()
- NextTime += Interval
- End While
- End Sub) With {.IsBackground = True}
- Public Shared Sub [Set](ByVal Milliseconds As Decimal)
- If EventThread.ThreadState = 12 Then
- Interval = Milliseconds * TicksPerMillisecond
- NextTime = Stopwatch.GetTimestamp + Interval
- EventThread.Start()
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement