Link to home
Start Free TrialLog in
Avatar of Jack_Jones
Jack_Jones

asked on

Help with making this into a random delay.

Hello Experts,

 My question is the current delay works great, but what I wanted to do was have a random delay in this, well maby not random but like if i put delay(.9) it would then do delay(.9) +/- .3 to give it delay of (.6) or delay of (12) thanks ahead of time, I am off for a few hours and will check back soon.

 Then if possible the same for rdelay(.4) would give +/- .1 so it would be like rdelay of (.3) or rdelay of (.6) thanks again.

Private Sub Delay(ByVal DelayInSeconds As Integer)
        Dim targetDT As DateTime = DateTime.Now.Add(TimeSpan.FromSeconds(DelayInSeconds))
        While targetDT > DateTime.Now
            System.Threading.Thread.Sleep(50) ' <-- very SMALL delay
            Application.DoEvents() ' <-- keep UI responsive
        End While
    End Sub

Private Sub rdelay(ByVal DelayInSeconds As Integer)
        Dim targetDT As DateTime = DateTime.Now.Add(TimeSpan.FromSeconds(DelayInSeconds))
        While targetDT > DateTime.Now
            System.Threading.Thread.Sleep(50) ' <-- very SMALL delay
            Application.DoEvents() ' <-- keep UI responsive
        End While
    End Sub

Open in new window

SOLUTION
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The above code will give you a random delay of +/- 50% of the submitted delay
Avatar of Mike Tomlinson
Something like?
Private Sub rdelay(ByVal DelayInSeconds As Double)
        Static R As New Random
        DelayInSeconds = DelayInSeconds * CDbl(R.Next(75, 126)) / CDbl(100)
        Dim targetDT As DateTime = DateTime.Now.Add(TimeSpan.FromSeconds(DelayInSeconds))
        While targetDT > DateTime.Now
            System.Threading.Thread.Sleep(50) ' <-- very SMALL delay
            Application.DoEvents() ' <-- keep UI responsive
        End While
    End Sub

Open in new window

Oh!....Hey Jeff!  =)
howdy... fancy meeting you here :)
Avatar of Jack_Jones
Jack_Jones

ASKER

Heya just woke up gonna poke around and test it, be back soon ;). Thanks for the quick help to.
Is there away I can display the delay in a message box? Just so I can see how they are different thanks again if that isn't to much of a problem.
You'd need to create a message box to do so (I know, obvious, right?)

    Private Sub ExecuteDelay(ByVal delayLength As Integer)
        Dim rand As New Random
        delayLength = (0.5 + rand.NextDouble()) * delayLength
        txtDelay.Text = String.Format("The delay is {0} ms.", delayLength.ToString)
        System.Threading.Thread.Sleep(delayLength)
        Application.DoEvents()
    End Sub
Yeah but i don't know how to put that into a message box lol
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both for your quick help! ;)