Responsive Delay Timer

SifuGreg

New member
Joined
Sep 16, 2020
Messages
1
Programming Experience
10+
Okay.. full disclosure. I was a VB programmer for years but never made it into .net. Somewhere along the way I got old and busy. I've decided to dust off the tools and tackle a project. This particular project requires something akin to the old doevents but is fighting me. Here would be a simplified scenario: Two text boxes with two integers. This is a simple process of the code reading the first integer and delaying that many seconds, playing a sound.. "sleeping" for the value of the second text box and playing a different sound. During this time, however, I need the program to be responsive. Now, I've gotten close by calling this code
VB.NET:
Public Sub ResponsiveSleep(ByRef MilliSeconds As Integer)
        Dim i As Integer
        Dim tenthseconds As Integer = MilliSeconds / 100 
        For i = 1 To tenthseconds 
            Threading.Thread.Sleep(MilliSeconds / 100) : Application.DoEvents() 
        Next i
    End Sub
but the math isn't working. if I put in a value of 10 in the first box (* 1000) then by gosh I get a 10 second delay. If I put in 3 seconds, I get about a 2 second delay and if I put in 20 seconds I get over 30 seconds. This brings up just a few questions:
  • Did I completely miss something (default)
  • Is this a function of looping
    • If I understand what I'm doing here is I'm chunking up the delay and looping it so the system can see other events
  • Is there a sexier way to achieve this "responsive sleep"
Thanks guys, I know I'm stuck in the past but seriously appreciate any guidance you can provide.
 
Back
Top