-1

I quickly wrote this little rinky-dink vb.net console app to demonstrate something to someone. When I got to looking at it I thought there must be a better way than eating up cycles by using

While True

...

End While

but I have no idea what it is. Any thoughts?

Module ControlExecutive
    Private WithEvents MyTimer As New Timers.Timer
    Sub Main()
        MyTimer.Interval = 10000
        Console.WriteLine("Start timer, interrupt every 10000 ms")
        MyTimer.Start()
        OuterLoop()
    End Sub

    Sub OuterLoop()
        While True
            'wait for timer interrupts
        End While
    End Sub

    Sub HandleTimerInterrupt(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
        Console.WriteLine(String.Format("Interrupt at {0}", DateTime.Now))
    End Sub
End Module
Dave
  • 169
  • 9
  • I would add a `Thread.Sleep` inside the loop in order to "go easy" on the CPU... – xfx Jan 24 '13 at 18:12

1 Answers1

3

Instead of (or inside) a loop for waiting, you should use System.Threading.Thread.Sleep(milliseconds). This will pause the thread without causing a CPU spike.

xpda
  • 15,585
  • 8
  • 51
  • 82