Resetting a timer in VB.NET

twyztyr

Gawd
Joined
Jun 7, 2004
Messages
984
Is it possible? I've been trying to figure it out. It seems to me that the .Start method and .Enabled = True assignment are exactly the same, just like .Stop and Enabled = False are the same. I would think that there should be some way to get the timer to reset.

I've already turned in my project, and couldn't figure this one out, but now it seems to have become a personal vendetta to beat this damn thing. Anyone have any suggestions on how to get a VB.NET timer to reset?
 
twyztyr said:
Is it possible? I've been trying to figure it out. It seems to me that the .Start method and .Enabled = True assignment are exactly the same, just like .Stop and Enabled = False are the same. I would think that there should be some way to get the timer to reset.

I've already turned in my project, and couldn't figure this one out, but now it seems to have become a personal vendetta to beat this damn thing. Anyone have any suggestions on how to get a VB.NET timer to reset?


A Timer is not a measurement device as you are thinking, rather it's like a kitchen timer that gives you an event after a certain amount of time.

If you are directly trying to measure the amount of time it takes to do something, it's pretty easy to use the DateTime and TimeSpan objects to do so. Here is a simple example that collects the total time from multiple measurements:
Code:
Module Module1

    Sub Main()
        Dim simp As New myStopwatch

        simp.StartTiming()
        System.Threading.Thread.Sleep(500)
        simp.StopTiming()

        Console.WriteLine("total: {0}    last: {1}", simp.TotalTimeSpan, simp.LastTimeSpan)

        simp.StartTiming()
        System.Threading.Thread.Sleep(200)
        simp.StopTiming()

        Console.WriteLine("total: {0}    last: {1}", simp.TotalTimeSpan, simp.LastTimeSpan)

        simp.StartTiming()
        System.Threading.Thread.Sleep(2000)
        simp.StopTiming()

        Console.WriteLine("total: {0}    last: {1}", simp.TotalTimeSpan, simp.LastTimeSpan)
        Console.ReadKey()
    End Sub

End Module



Class myStopwatch
    Private StartTime As DateTime
    Public LastTimeSpan As TimeSpan
    Public TotalTimeSpan As TimeSpan

    Public Sub StartTiming()
        StartTime = DateTime.Now
    End Sub

    Public Sub StopTiming()
        LastTimeSpan = DateTime.Now - StartTime
        TotalTimeSpan += LastTimeSpan
    End Sub
End Class

If you are using the 2.0 Framework, there is a Stopwatch object built in, and it offers a little more functionality, but on the whole such things are only a couple lines of code.
 
Alright, the project was this. I had to use the timer to move a graphic object across a form when a start button was pressed. When a pause button was pushed, the graphic would stop, when it was restarted, it would pick up where it left off. When the stop button was pressed, I got the graphic object to return to its start location, but when the start button was pressed again, it picked up where it left off instead of at the beginning.

Now that I'm thinking about it, I don't think it is a problem with the timer at all, but lies somewhere else. As I've already turned it in, I don't really have to worry about it, I'm just curious. I might try to fix it sometime though, just to see if I can. Thanks for the replies though.
 
twyztyr said:
As I've already turned it in, I don't really have to worry about it, I'm just curious.

Fair enough...

An object, that when added to a controls collection, will bounce around the client area of the parent control. If the parent changes or resizes, the bouncer will reposition itself to a new valid start position. It's C#, but all the framework calls would be the same in VB...

Add a few of those to your winform... :)
Code:
using System;
using System.Windows.Forms;

namespace bounce
{
    public partial class Bouncer : UserControl
    {
        private int incX, incY;
        private Control oldParent = null;
        public Timer StepTimer;
        private EventHandler resizeHandler;

        public Bouncer()
        {
            InitializeComponent();
            
            Random rnd = new Random();
            incX = rnd.Next(7) - 4;
            incY = rnd.Next(7) - 4;

            this.ParentChanged += new EventHandler(Bouncer_ParentChanged);
            this.resizeHandler = new EventHandler(Parent_Resize);

            StepTimer = new Timer();
            StepTimer.Interval = 50;
            StepTimer.Tick += new EventHandler(Step);
        }
        void Parent_Resize(object sender, EventArgs e)
        {
            if (this.Left > Parent.Width-this.Width || this.Top > Parent.Height-this.Height)
                SetRandomPosition();
        }

        void Bouncer_ParentChanged(object sender, EventArgs e)
        {
            if (oldParent != null && oldParent.IsDisposed == false)
                oldParent.Resize -= this.resizeHandler;
            if (this.Parent != null)
            {
                this.Parent.Resize += this.resizeHandler;
                SetRandomPosition();
                StepTimer.Start();
                oldParent = this.Parent;
            }
            else
                StepTimer.Stop();
        }

        private void SetRandomPosition()
        {
            if (this.Parent.Height < this.Height || this.Parent.Width < this.Width)
                throw new Exception("Parent client area too small");
            Random rnd = new Random();
            this.Top = rnd.Next(this.Parent.Height - this.Height);
            this.Left = rnd.Next(this.Parent.Width - this.Width);
            if (this.Top + incY < 0 || this.Top + incY > Parent.Height - this.Height)
                incY = -incY;
            if (this.Left + incX < 0 || this.Left + incX > Parent.Width - this.Width)
                incX = -incX;
        }

        private void Step(object Sender, EventArgs e)
        {
            if (this.Top + incY < 0 || this.Top + incY > Parent.Height - this.Height)
                incY = -incY;
            if (this.Left + incX < 0 || this.Left + incX > Parent.Width - this.Width)
                incX = -incX;
            this.Top += incY;
            this.Left += incX;
        }
    }
}
 
Back
Top