I can't figure out how to solve this problem.

K

ken

Hello,
I can't figure out how to solve this problem. I modified the timer
example given in the help section. It increments a count every 3
millisecond in order to simulate a tank being filled with a pump rated
at 180 GPM. It works fine on the first pass but, when I select the
stop command and then restart it the incremented values are a factor
of 2 ie start 2. 4. 6..... etc. Each time I stop and restart the
values incremented by the number of times I stop the count. Count is
stopped and started four times the values are multiples of 4, stopped
and started five times. multiples of 5 etc. Any ideas? Thanks for the
help.
Regards,
Ken

Public Class Form4

Private Shared Timer1 As New System.Windows.Forms.Timer()
Private Shared alarmCounter As Integer = 0
Private Shared exitFlag As Boolean = False
Dim DisplayFill As Integer = 0
' This is the method to run when the timer is raised.
Public Sub TimerEventProcessor(ByVal myObject As Object, _
ByVal myEventArgs As EventArgs)
Timer1.Stop()

' Displays a message box asking whether to continue running
the timer.
'If MessageBox.Show("Continue running?", "Count is: " &
alarmCounter, _
' MessageBoxButtons.YesNo) =
DialogResult.Yes Then
' Restarts the timer and increments the counter.

alarmCounter += 1
DisplayFill = alarmCounter
txtTimer1.Text = DisplayFill
txtTimer1.Refresh()
Timer1.Enabled = True
'Else
' Stops the timer.
exitFlag = True
'End If
End Sub

Public Sub Main()
' Adds the event and the event handler for the method that
will
' process the timer event to the timer.
AddHandler Timer1.Tick, AddressOf TimerEventProcessor

alarmCounter = 0
' Sets the timer interval to 3 milliseconds.

Timer1.Interval = 333
Timer1.Start()

' Runs the timer, and raises the event.
While exitFlag = False
' Processes all the events in the queue.
Application.DoEvents()
End While

End Sub

Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdStart.Click
Main()
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdStop.Click
Timer1.Stop()
End Sub

End Class
 
G

Guest

ken,

Every time the Start button is clicked it executes Main.

Main in turn adds a handler for the timer.

So every time you click Start another handler is added that processes the
timer's Tick event. The Tick event is being handled multiple times per tick.

You might try moving the loop code to its own sub and calling that sub from
Main and from the Start button's click event.

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top