Timer Questions

ravoll

Well-known member
Joined
Aug 26, 2010
Messages
50
Programming Experience
Beginner
Hi all,
How can I get a timer to run one interval,one interval only, and stop? Until now I have only used timers to keep running until I say to stop.

Also I need a timerthat will start at the beginning of, and run until any given process is completed.Upon completion it should shut off and tell me how long the process took.
Kinda like a stop watch with a Stop / Start "Trigger".
 
Hi all,
How can I get a timer to run one interval,one interval only, and stop? Until now I have only used timers to keep running until I say to stop.
A bit of logic. You Start the Timer, it Ticks. If you want it to Stop then Stop it. Call Stop in the Tick event handler. Alternatively, you could use the System.Timers.Timer, which stops after one Elapsed event by default, instead of a System.Windows.Forms.Timer.
Also I need a timerthat will start at the beginning of, and run until any given process is completed.Upon completion it should shut off and tell me how long the process took.
Kinda like a stop watch with a Stop / Start "Trigger".
That's not a Timer. A Timer is like an alarm clock. If you want a stopwatch then you use, as you would expect, a Stopwatch. It has all the methods and properties you would expect of an in-code stopwatch, like Start, Stop and Elapsed.
 
Ok thanks,
I'll look around and see if I can find code for a stop watch.

Apparently just typing "Timer1.Stop" in the timer event handler doesn't work.

In my toolbox I can only find the System.Windows.Forms.Timer.
where do I find the System.Timers.Timer?
 
VB.NET:
    Private indicator As Long = 0
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        indicator += 1
        If indicator Mod 2 Then
            Me.PictureBox1.Image = My.Resources.green
        Else
            Me.PictureBox1.Image = My.Resources.red
        End If
        If indicator = 2 Then
            Timer1.Stop()
        End If
    End Sub
 
Apparently just typing "Timer1.Stop" in the timer event handler doesn't work.
Of course it does.
In my toolbox I can only find the System.Windows.Forms.Timer.
where do I find the System.Timers.Timer?
Where do you find every other type? String isn't in your Toolbox. Integer isn't in your Toolbox. Most types aren't in your Toolbox, but you can still use them in code. This is no different.

That said, the Timers.Timer is a component, so you can add it to the Toolbox if you want, as you can with any component. In fact, it used to be by default in VS.NET 2003. I guess they figured having both by default was confusing.
 
Of course it does.Where do you find every other type? String isn't in your Toolbox. Integer isn't in your Toolbox. Most types aren't in your Toolbox, but you can still use them in code. This is no different.

That said, the Timers.Timer is a component, so you can add it to the Toolbox if you want, as you can with any component. In fact, it used to be by default in VS.NET 2003. I guess they figured having both by default was confusing.

Sounds good ,
How do I do this?
 
Yes I saw the code you posted.It works fine.
I only had the LED 's running shortly to check visually if my timers were firing when they were supposed to.I won't be using the LED's in my app.
I need something that knows when the timer1 interval is complete,maybe a "Do while Timer1.Interval < (set interval) thingy.Have tried that but I already have a "While" condition in the timer event and when I put a second ,I have conflicts.

jmcilhinney's code would probably work ,but i still haven't figured out how and where to use it.

will keep trying
 
I think you need to show us YOUR code because there's definitely something wrong with it. You shouldn't have a loop in your Tick event handler for a start.

Hi,Have set both timers back to how I need them for now,without the LED codes,cause like I said was just an expiriment to see if the timers really started when prompted .

Code:
VB.NET:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        While Timer1.Interval < 1000
            TextBox1.Text = imp_count

        End While
        imp_count = 0
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If TextBox1.Text > 0 Then
            ListBox1.Items.Add(TextBox1.Text)
            ListBox2.Items.Add((TextBox1.Text / 6) * 60)
        End If

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = Rev
        If TextBox1.Text > 1 Then
            Timer1.Enabled = True
            Timer2.Enabled = True
        End If
    End Sub

So I need to have the Timer1 only run one interval and stop.
Timer2 is set right now at 500milli intervals in the form1.load dialog for now
I also want to shut timer2 down when timer1 stops,but one interval later.If timer2 stops at the same time timer1 does,then it does'nt "add" the last TextBox1.Text to the lists.

been racking brains for 3 days now,have tried all kinds of stuff and all suggestions.The only thing that came close to what I want was the LEDS, but the last count was missed .
 
Last edited:
I case your wondering,I'm trying to accomplish this:

http://www.vbdotnetforums.com/vb-net-general-discussion/43683-counting-impulses.html

Since I've had no replies on this, I figure it's not possible,or to complicated ,so I'm
trying this as a new aproach.I want to see each single impulse/signal till top speed.About 383 impulses.Only takes about a second.Since the count starts over at zero ,instead of holding current after every interval,I want to run it over one interval,only one interval ,and it should stop.During the timer1 interval,Timer2 takes a "sample" of the impulse count,and writes it to a listbox.By taking more samples per interval (every 20ms=50 "samples" per timer1 interval),I can calculate the increase in impulse's ,which is directly proportional to the increase in RPM.Then I can later calculate the change in RPM per sec/sec.(Angular Acceleration).

side note: due to the unreliability of the windows timers,this will be done ten times in a row,and further calculations are based on an average.Have done this before,with the LED version,and all differences are within 100 RPM's.

Close enough for me,but I really get the timer.1 to stop on it's own,other wise it reaches it's peak impulse and starts over .By the time my slow finger presses stop it's racked up 20 or 30 new impulses that are really lies.

If I could accomplish what I mentioned in the unanswered thread,I woudn't need to stop after every interval .I could just let it run,and Timer2 would just record the top or "steady" speed
 
Last edited:
The first mistake you're making is assuming that the Interval of the Timer changes. The Interval is the number of milliseconds between Tick events and that's it. It doesn't change unless you set it to a different value. If you set it to 1000 then it will be 1000 all the time.

As for stopping the Timer, I don't see where you're doing that anywhere in your code so I can't comment on what you were doing wrong except to guess that you were trying to do it in that While loop that shouldn't be there in the first place. Either you never entered the While loop because the Interval was never less than 1000 or you never exited because the Interval was always less than 1000.
 
The first mistake you're making is assuming that the Interval of the Timer changes. The Interval is the number of milliseconds between Tick events and that's it. It doesn't change unless you set it to a different value. If you set it to 1000 then it will be 1000 all the time.

As for stopping the Timer, I don't see where you're doing that anywhere in your code so I can't comment on what you were doing wrong except to guess that you were trying to do it in that While loop that shouldn't be there in the first place. Either you never entered the While loop because the Interval was never less than 1000 or you never exited because the Interval was always less than 1000.

Go back and read my previous posts
I never assumed the timer interval changes( unless I change it myself)but my impulses do , and that is what I want to capture.
So:
"While" the timer interval is less than "1000" it displays the impulses in the textbox."1000" is approximately how long I need to reach top speed.After the 1000 interval, it starts over at zero,but I want it to stop after1 interval.

I have to agree , you cannot see where I'm trying to stop the timer after the first interval. I'm not in this code because...I don't know how....that is why I'm here asking!

It's as simple as this.This code is what I'm using ,and I need something in "this" code to stop Timer1 after the first interval.
 
Back
Top