views:

230

answers:

1

What's the easiest way to run a periodic task in the background in VB?

For example: Update a label every second with the current time, while still allowing the form to be available.

+6  A: 
  1. Add a Timer component to your form
  2. Set its Interval to the time between updates
  3. Enable the timer (by setting its Enabled property or calling its Start method)
  4. Handle its Tick event by doing whatever updates are necessary.

Your handler would look something like this:

Private Sub Timer1_Tick(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles Timer1.Tick

   label1.Text = GetCurrentStatus()

End Sub
Daniel LeCheminant
Yep. By far the easiest.
Chris Lively
Also, don't forget that the `Interval` is specified in milliseconds...
Daniel LeCheminant