1

Topic: Brother Erryn's VB Timer Alternatives

Hi, Brother Erryn!

I am JamRoll, an intermediate programmer in VB.  I am creating a game of my own (no name...yet).  I am currently using the clunky timer control provided by vb express 2008.  I have viewed your posts regarding timers...but I am confused.  I would have posted to yours posts, but it says I don't have permission to...

Currently I have

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

Um...how do I incorporate your method into my program???
Or am I just way off base here?  LOL big_smile

Sincerely,

JamRoll

2

Re: Brother Erryn's VB Timer Alternatives

Wow, that's been a while. I guess you're talking about my old articles on GPWiki (also posted here)? Those were written years ago, back in the days of .NET 1.0...today I would use something else. If you're wanting something to automatically fire at regular intervals, consider using the DispatcherTimer (.NET 3.0) class.

Here's an example of it in use:

timer.Interval = New TimeSpan(0, 0, 1)
timer.IsEnabled = True
AddHandler timer.Tick, AddressOf UpdateUI
timer.Start()

If you just want to get an accurate measure of how much time has passed since the last time you checked, use the Stopwatch (.NET 2.0) class.

An example of using the Stopwatch:

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

... do stuff ...

stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;

Does this sort of answer your question? You didn't really say anything about what you're trying to do with your timer in this case.

Are you playing?
http://www.atomicmonks.com/sig.php?a=av&pname=Brother%20Erryn