Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Thread.Sleep Replacer

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.
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.
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.
Threading.Timer https://msdn.microsoft.com/en-us/library....110).aspx
System.Timers.Timer vs System.Threading.Timer http://stackoverflow.com/questions/14168...ding-timer
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(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
(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.
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/14455...-and-await
https://msdn.microsoft.com/en-us/magazine/jj991977.aspx
(Jan 4, 2017 07:39 PM)Sn0wX Wrote:  you aint got a clue have you

99% of this forum dont have 😉
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(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.
you mean enough copy pasta.
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(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. ^^
(Jan 5, 2017 10:15 AM)Barret Wrote:  Hehehe, if only you knew who i really was you would know. ^^

same same and i dont think you know who i was.
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
(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.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by SteamMar 12, 2019 04:03 PM 6 415 Mar 12, 2019 04:03 PM
Last Post: Steam
[.NET] by SIM-2362662Nov 10, 2015 11:48 AM 10 510 Nov 10, 2015 11:48 AM
Last Post: IAmNotHere
[C#] by neiqhtworksOct 24, 2013 05:01 PM 4 268 Oct 24, 2013 05:01 PM
Last Post: iVision