Japt

PrecisionTimer - VB.NET

Jul 18th, 2016
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '------------------
  2. 'Creator: aeonhack
  3. 'Site: elitevs.net
  4. 'Created: 11/30/2011
  5. 'Changed: 11/30/2011
  6. 'Version: 1.0.0
  7. '------------------
  8. Class PrecisionTimer
  9. Implements IDisposable
  10.  
  11. Private _Enabled As Boolean
  12. ReadOnly Property Enabled() As Boolean
  13. Get
  14. Return _Enabled
  15. End Get
  16. End Property
  17.  
  18. Private Handle As IntPtr
  19. Private TimerCallback As TimerDelegate
  20.  
  21. <DllImport("kernel32.dll", EntryPoint:="CreateTimerQueueTimer")> _
  22. Private Shared Function CreateTimerQueueTimer( _
  23. ByRef handle As IntPtr, _
  24. ByVal queue As IntPtr, _
  25. ByVal callback As TimerDelegate, _
  26. ByVal state As IntPtr, _
  27. ByVal dueTime As UInteger, _
  28. ByVal period As UInteger, _
  29. ByVal flags As UInteger) As Boolean
  30. End Function
  31.  
  32. <DllImport("kernel32.dll", EntryPoint:="DeleteTimerQueueTimer")> _
  33. Private Shared Function DeleteTimerQueueTimer( _
  34. ByVal queue As IntPtr, _
  35. ByVal handle As IntPtr, _
  36. ByVal callback As IntPtr) As Boolean
  37. End Function
  38.  
  39. Delegate Sub TimerDelegate(ByVal reserve As IntPtr, ByVal reserve As Boolean)
  40.  
  41. Sub Create(ByVal dueTime As UInteger, ByVal period As UInteger, ByVal callback As TimerDelegate)
  42. If _Enabled Then Return
  43.  
  44. TimerCallback = callback
  45. Dim Success As Boolean = CreateTimerQueueTimer(Handle, IntPtr.Zero, TimerCallback, IntPtr.Zero, dueTime, period, 0)
  46.  
  47. If Not Success Then ThrowNewException("CreateTimerQueueTimer")
  48. _Enabled = Success
  49. End Sub
  50.  
  51. Sub Delete()
  52. If Not _Enabled Then Return
  53. Dim Success As Boolean = DeleteTimerQueueTimer(IntPtr.Zero, Handle, IntPtr.Zero)
  54.  
  55. If Not Success AndAlso Not Marshal.GetLastWin32Error = 997 Then
  56. ThrowNewException("DeleteTimerQueueTimer")
  57. End If
  58.  
  59. _Enabled = Not Success
  60. End Sub
  61.  
  62. Private Sub ThrowNewException(ByVal name As String)
  63. Throw New Exception(String.Format("{0} failed. Win32Error: {1}", name, Marshal.GetLastWin32Error))
  64. End Sub
  65.  
  66. Private Sub Dispose() Implements IDisposable.Dispose
  67. Delete()
  68. End Sub
  69. End Class
Add Comment
Please, Sign In to add comment