• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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

0
Jack_Jones
Asked:
Jack_Jones
  • 4
  • 4
  • 3
2 Solutions
 
Jeff CertainCommented:
   Private Sub ExecuteDelay(ByVal delayLength As Integer)
        Dim rand As New Random
        delayLength = (0.5 + rand.NextDouble()) * delayLength
        System.Threading.Thread.Sleep(delayLength)
        Application.DoEvents()
    End Sub
0
 
Jeff CertainCommented:
The above code will give you a random delay of +/- 50% of the submitted delay
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Oh!....Hey Jeff!  =)
0
 
Jeff CertainCommented:
howdy... fancy meeting you here :)
0
 
Jack_JonesAuthor Commented:
Heya just woke up gonna poke around and test it, be back soon ;). Thanks for the quick help to.
0
 
Jack_JonesAuthor Commented:
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.
0
 
Jeff CertainCommented:
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
0
 
Jack_JonesAuthor Commented:
Yeah but i don't know how to put that into a message box lol
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could display it in the Immediate Window with Debug.Print():
Private Sub rdelay(ByVal DelayInSeconds As Double)
    Static R As New Random
    Debug.Print("Original: " & DelayInSeconds)
    DelayInSeconds = DelayInSeconds * CDbl(R.Next(75, 126)) / CDbl(100)
    Debug.Print("Randomized: " & DelayInSeconds)
    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

0
 
Jack_JonesAuthor Commented:
Thank you both for your quick help! ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now