# Visual Basic > Visual Basic .NET >  Responsive Delay Timer

## Frankti

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 



```
Public Sub ResponsiveSleep(ByRef MilliSeconds As Integer)
Dim i As Integer
Dim tenthseconds As Integer = MilliSeconds / 100 
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"

----------


## schoemr

Hi Frank,

The sleep function will definitely interfere with user inputs as your programme will be unresponsive as long as the sleep function is running.. 

If you want to pause a thread and still allow user inputs, I suggest you use maybe a timer...

----------


## .paul.

As Schoemr says, a timer, or threading is the way to do this. Running sleep on the UI thread will freeze your app.

----------


## PlausiblyDamp

> 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 
> 
> 
> 
> ```
> Public Sub ResponsiveSleep(ByRef MilliSeconds As Integer)
> Dim i As Integer
> Dim tenthseconds As Integer = MilliSeconds / 100 
> Threading.Thread.Sleep(MilliSeconds / 100) : Application.DoEvents() 
> ...


Depending on the version of VB.Net you might be abel to use https://learn.microsoft.com/en-us/do...sks.task.delay instead of Thread.Sleep

----------

