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.
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The above code will give you a random delay of +/- 50% of the submitted delay
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
Oh!....Hey Jeff! =)
howdy... fancy meeting you here :)
ASKER
Heya just woke up gonna poke around and test it, be back soon ;). Thanks for the quick help to.
ASKER
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.Sl eep(delayL ength)
Application.DoEvents()
End Sub
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.Sl
Application.DoEvents()
End Sub
ASKER
Yeah but i don't know how to put that into a message box lol
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both for your quick help! ;)