Hack Forums
[.NET] Thread.Sleep Replacer - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Thread.Sleep Replacer (/showthread.php?tid=5514274)



Thread.Sleep Replacer - Barret - Jan 4, 2017

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.

Code
While True
     DelayedTask(AddressOf Me.DoTask, 250).WaitOne()
End While

Code
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 Function

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
Code
Private Sub DoTask(resetEvent As ManualResetEvent)
      
         ...code...

        ' All done, signal to clean up
        resetEvent.Set()
    End Sub


Let me know what you think about it and please do me a favor if you don't understand, don't reply.


* - Sn0wX - Jan 4, 2017

urgh...this is some thing beyond awful. I dont think you even under stand the difference between threads/timers let alone other async methods already created that can handle things for us.


* - Malum™ - Jan 4, 2017

You have the right idea and are going in the right direction. Just look more into it and see if you can clean it up a bit.


* - HFDone - Jan 4, 2017

Threading.Timer https://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx
System.Timers.Timer vs System.Threading.Timer http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer


* - Sn0wX - Jan 4, 2017

(Jan 4, 2017 06:39 PM)Malum™ Wrote:  You have the right idea and are going in the right direction. Just look more into it and see if you can clean it up a bit.

you aint got a clue have you


* - Malum™ - Jan 4, 2017

(Jan 4, 2017 07:39 PM)Sn0wX Wrote:  you aint got a clue have you

No need to be rude. Be constructive. You don't have to spoon feed. Just lead people in the right direction or elaborate. Hell, even be sassy, just don't be an asshole.


* - Zer0Memo0ry - Jan 5, 2017

It's ages since I even touched .net but I remember using async tasks worked great when Thread.Sleep() gave bad results.
http://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx


* - HFDone - Jan 5, 2017

(Jan 4, 2017 07:39 PM)Sn0wX Wrote:  you aint got a clue have you

99% of this forum dont have 😉


* - Barret - Jan 5, 2017

(Jan 5, 2017 09:44 AM)HFDone Wrote:  99% of this forum dont have 😉

No argu there ^^ anyway ive seen enough. This snipplet is part of a bigger TCP server/client project that uses serialized packet system layer. Basically you can send any custom class that is serialized from within client or server, each packet is universal which can be compressed and validated with a packet checksum.


* - HFDone - Jan 5, 2017

you mean enough copy pasta.


* - Barret - Jan 5, 2017

(Jan 5, 2017 10:14 AM)HFDone Wrote:  you mean enough copy pasta.

Hehehe, if only you knew who i really was you would know. ^^


* - HFDone - Jan 5, 2017

(Jan 5, 2017 10:15 AM)GrimEdittion Wrote:  Hehehe, if only you knew who i really was you would know. ^^

same same and i dont think you know who i was.


* - Barret - Jan 5, 2017

(Jan 5, 2017 10:28 AM)HFDone Wrote:  same same and i dont think you know who i was.

Well that case, tell me more.