TizzyT

MillisecondTimer

Sep 7th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.52 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2. Public Class MsTimer
  3.     Private Delegate Sub TimerEventDel(id As Integer, msg As Integer, user As IntPtr, dw1 As Integer, dw2 As Integer)
  4.     Private Const TIME_PERIODIC As Integer = 1
  5.     Private Const EVENT_TYPE As Integer = TIME_PERIODIC
  6.     Private mAction As Action
  7.     Private mTimerId As Integer
  8.     Private mHandler As TimerEventDel
  9.     <DllImport("winmm.dll")> _
  10.     Private Shared Function timeBeginPeriod(msec As Integer) As Integer
  11.     End Function
  12.     <DllImport("winmm.dll")> _
  13.     Private Shared Function timeEndPeriod(msec As Integer) As Integer
  14.     End Function
  15.     <DllImport("winmm.dll")> _
  16.     Private Shared Function timeSetEvent(delay As Integer, resolution As Integer, handler As TimerEventDel, user As IntPtr, eventType As Integer) As Integer
  17.     End Function
  18.     <DllImport("winmm.dll")> _
  19.     Private Shared Function timeKillEvent(id As Integer) As Integer
  20.     End Function
  21.     Public Sub New(action As Action, delay As Integer)
  22.         mAction = action
  23.         timeBeginPeriod(1)
  24.         mHandler = New TimerEventDel(AddressOf TimerCallback)
  25.         mTimerId = timeSetEvent(delay, 0, mHandler, IntPtr.Zero, EVENT_TYPE)
  26.     End Sub
  27.     Public Sub [Stop]()
  28.         Dim err As Integer = timeKillEvent(mTimerId)
  29.         timeEndPeriod(1)
  30.         System.Threading.Thread.Sleep(10)
  31.     End Sub
  32.     Private Sub TimerCallback(id As Integer, msg As Integer, user As IntPtr, dw1 As Integer, dw2 As Integer)
  33.         If mTimerId <> 0 Then mAction()
  34.     End Sub
  35. End Class
Advertisement
Add Comment
Please, Sign In to add comment