Advertisement
TizzyT

GameTimer -TizzyT

Oct 17th, 2016
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.06 KB | None | 0 0
  1. Public Class GameTimer
  2.     <Runtime.InteropServices.DllImport("winmm.dll")>
  3.     Private Shared Function timeBeginPeriod(ByVal msec As UInteger) As UInteger : End Function
  4.     <Runtime.InteropServices.DllImport("winmm.dll")>
  5.     Private Shared Function timeEndPeriod(ByVal msec As UInteger) As UInteger : End Function
  6.     <Runtime.InteropServices.DllImport("winmm.dll")>
  7.     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
  8.     <Runtime.InteropServices.DllImport("winmm.dll")>
  9.     Private Shared Function timeKillEvent(ByVal id As UInteger) As UInteger : End Function
  10.     Private Delegate Sub TimerEventDel(ByVal id As UInteger, ByVal msg As UInteger, ByVal user As IntPtr, ByVal dw1 As IntPtr, ByVal dw2 As IntPtr)
  11.     Public Shared Event Elapsed()
  12.     Private Shared ReadOnly TicksPerMillisecond As Decimal = Stopwatch.Frequency / 1000
  13.     Private Shared ReadOnly MicroWait As New Threading.AutoResetEvent(False)
  14.     Private Shared NextTime As Decimal = Long.MaxValue
  15.     Private Shared Interval As Decimal = Stopwatch.Frequency
  16.     Private Shared EventThread As New Threading.Thread(Sub()
  17.                                                            While True
  18.                                                                Dim Ms As Integer = Math.Round(((NextTime - Stopwatch.GetTimestamp) / TicksPerMillisecond) - 1)
  19.                                                                If Ms > 1 Then
  20.                                                                    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)
  21.                                                                                                              MicroWait.Set()
  22.                                                                                                              While timeKillEvent(id) <> 0UI : Threading.Thread.Sleep(1) : End While
  23.                                                                                                          End Sub), IntPtr.Zero, 0)
  24.                                                                    MicroWait.WaitOne()
  25.                                                                End If
  26.                                                                While Stopwatch.GetTimestamp < NextTime : End While
  27.                                                                RaiseEvent Elapsed()
  28.                                                                NextTime += Interval
  29.                                                            End While
  30.                                                        End Sub) With {.IsBackground = True}
  31.     Public Shared Sub [Set](ByVal Milliseconds As Decimal)
  32.         If EventThread.ThreadState = 12 Then
  33.             Interval = Milliseconds * TicksPerMillisecond
  34.             NextTime = Stopwatch.GetTimestamp + Interval
  35.             EventThread.Start()
  36.         End If
  37.     End Sub
  38. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement