I would like to know from the guys that are what more advanced in programming .Net area if this would be a suitable replacer for the Thread.Sleep() function since in general this function is not ideal (inaccurate) and has no control to stop earlier either.
Imagine the following:
The main loop with tasks that should be delayed each call and wait for the task to finish.
The function can be used several ways it makes use of a timer with desirable precision,
also the ResetEvent makes it possible to block the thread and stop earlier if required.
The method that the timer will call
Let me know what you think about it and please do me a favor if you don't understand, don't reply.
Imagine the following:
The main loop with tasks that should be delayed each call and wait for the task to finish.
Code
While True
DelayedTask(AddressOf Me.DoTask, 250).WaitOne()
End WhileCode
Public Shared Function DelayedTask(m As Action(Of ManualResetEvent), msecs As Double) As ManualResetEvent
Dim e As New ManualResetEvent(False), t As New Timers.Timer(msecs) With {.AutoReset = False}
Try
AddHandler t.Elapsed, Sub(obj, args) m.Invoke(e)
t.Start()
Return e
Finally
e.WaitOne()
t.Stop()
t.Dispose()
End Try
End FunctionThe function can be used several ways it makes use of a timer with desirable precision,
also the ResetEvent makes it possible to block the thread and stop earlier if required.
The method that the timer will call
Code
Private Sub DoTask(resetEvent As ManualResetEvent)
...code...
' All done, signal to clean up
resetEvent.Set()
End SubLet me know what you think about it and please do me a favor if you don't understand, don't reply.

