Stop Method

Timer.Stop Method ()


Stops raising the Elapsed event by setting Enabled to false.

Namespace:   System.Timers
Assembly:  System (in System.dll)

Public Sub Stop

You can also stop timing by setting Enabled to false.


The signal to raise the Elapsed event is always queued for execution on a ThreadPool thread, so the event-handling method might run on one thread at the same time that a call to the Stop method runs on another thread. This might result in the Elapsed event being raised after the Stop method is called. The code example in the next section shows one way to work around this race condition.

The following example instantiates a System.Timers.Timer object that fires its Timer.Elapsed event every two seconds (2,000 milliseconds), sets up an event handler for the event, and starts the timer. The event handler displays the value of the ElapsedEventArgs.SignalTime property each time it is raised. When the user presses the Enter key, the application calls the Stop method before terminating the application.

Imports System.Timers

Public Module Example
    Private aTimer As System.Timers.Timer

    Public Sub Main()

      Console.WriteLine("{0}Press the Enter key to exit the application...{0}",
      Console.WriteLine("The application started at {0:HH:mm:ss.fff}",

      Console.WriteLine("Terminating the application...")
    End Sub

    Private Sub SetTimer()
        ' Create a timer with a two second interval.
        aTimer = New System.Timers.Timer(2000)
        ' Hook up the Elapsed event for the timer. 
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
        aTimer.AutoReset = True
        aTimer.Enabled = True
    End Sub

    ' The event handler for the Timer.Elapsed event. 
    Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
        Console.WriteLine("The Elapsed event was raised at {0:HH:mm:ss.fff}",
    End Sub 
End Module
' The example displays output like the following:
'       Press the Enter key to exit the application...
'       The application started at 09:40:29.068
'       The Elapsed event was raised at 09:40:31.084
'       The Elapsed event was raised at 09:40:33.100
'       The Elapsed event was raised at 09:40:35.100
'       The Elapsed event was raised at 09:40:37.116
'       The Elapsed event was raised at 09:40:39.116
'       The Elapsed event was raised at 09:40:41.117
'       The Elapsed event was raised at 09:40:43.132
'       The Elapsed event was raised at 09:40:45.133
'       The Elapsed event was raised at 09:40:47.148
'       Terminating the application...

The following code example shows one way to prevent the thread that calls the Stop method from continuing until a currently executing Elapsed event ends, and also to prevent two Elapsed events from executing the event handler at the same time (often referred to as reentrancy).

The example executes 100 test runs. Each time the test is run, the timer is started with an interval of 150 milliseconds. The event handler uses the Thread.Sleep method to simulate a task that randomly varies in length from 50 to 200 milliseconds. The test method also starts a control thread that waits for a second and then stops the timer. If an event is being handled when the control thread stops the timer, the control thread must wait until the event is finished before proceeding.

The Interlocked.CompareExchange(Int32, Int32, Int32) method overload is used to avoid reentrancy and to prevent the control thread from continuing until an executing event ends. The event handler uses the CompareExchange(Int32, Int32, Int32) method to set a control variable to 1, but only if the value is currently zero. This is an atomic operation. If the return value is zero, the control variable has been set to 1 and the event handler proceeds. If the return value is non-zero, the event is simply discarded to avoid reentrancy. (If it were necessary to execute every event, the Monitor class would be a better way to synchronize the events.) When the event handler ends, it sets the control variable back to zero. The example records the total number of events that executed, that were discarded because of reentrancy, and that occurred after the Stop method was called.

The control thread uses the CompareExchange(Int32, Int32, Int32) method to set the control variable to -1 (minus one), but only if the value is currently zero. If the atomic operation returns non-zero, an event is currently executing. The control thread waits and tries again. The example records the number of times the control thread had to wait for an event to finish.

Imports System
Imports System.Timers
Imports System.Threading

Public Module Test

    ' Change these values to control the behavior of the program.
    Private testRuns As Integer = 100 
    ' Times are given in milliseconds:
    Private testRunsFor As Integer = 500
    Private timerIntervalBase As Integer = 100
    Private timerIntervalDelta As Integer = 20

    ' Timers.
    Private WithEvents Timer1 As New System.Timers.Timer
    Private WithEvents Timer2 As New System.Timers.Timer
    Private currentTimer As System.Timers.timer

    Private rand As New Random()

    ' This is the synchronization point that prevents events
    ' from running concurrently, and prevents the main thread 
    ' from executing code after the Stop method until any 
    ' event handlers are done executing.
    Private syncPoint As Integer = 0

    ' Count the number of times the event handler is called,
    ' is executed, is skipped, or is called after Stop.
    Private numEvents As Integer = 0
    Private numExecuted As Integer = 0
    Private numSkipped As Integer = 0
    Private numLate As Integer = 0

    ' Count the number of times the thread that calls Stop
    ' has to wait for an Elapsed event to finish.
    Private numWaits As Integer = 0

    <MTAThread> _
    Sub Main()
        For i As Integer = 1 To testRuns
            Console.Write(vbCr & "Test {0}/{1}    ", i, testRuns)

        Console.WriteLine("{0} test runs completed.", testRuns)
        Console.WriteLine("{0} events were raised.", numEvents)
        Console.WriteLine("{0} events executed.", numExecuted)
        Console.WriteLine("{0} events were skipped for concurrency.", numSkipped)
        Console.WriteLine("{0} events were skipped because they were late.", numLate)
        Console.WriteLine("Control thread waited {0} times for an event to complete.", numWaits)
    End Sub

    Sub TestRun()
        ' Set syncPoint to zero before starting the test 
        ' run. 
        syncPoint = 0

        ' Test runs alternate between Timer1 and Timer2, to avoid
        ' race conditions between tests, or with very late events.
        If currentTimer Is Timer1 Then
            currentTimer = Timer2
            currentTimer = Timer1
        End If

        currentTimer.Interval = timerIntervalBase _
            - timerIntervalDelta + rand.Next(timerIntervalDelta * 2)
        currentTimer.Enabled = True

        ' Start the control thread that shuts off the timer.
        Dim t As New Thread(AddressOf ControlThreadProc)

        ' Wait until the control thread is done before proceeding.
        ' This keeps the test runs from overlapping.

    End Sub

    Private Sub ControlThreadProc()
        ' Allow the timer to run for a period of time, and then 
        ' stop it.

        ' The 'counted' flag ensures that if this thread has
        ' to wait for an event to finish, the wait only gets 
        ' counted once.
        Dim counted As Boolean = False

        ' Ensure that if an event is currently executing,
        ' no further processing is done on this thread until
        ' the event handler is finished. This is accomplished
        ' by using CompareExchange to place -1 in syncPoint,
        ' but only if syncPoint is currently zero (specified
        ' by the third parameter of CompareExchange). 
        ' CompareExchange returns the original value that was
        ' in syncPoint. If it was not zero, then there's an
        ' event handler running, and it is necessary to try
        ' again.
        While Interlocked.CompareExchange(syncPoint, -1, 0) <> 0 
            ' Give up the rest of this thread's current time
            ' slice. This is a naive algorithm for yielding.

            ' Tally a wait, but don't count multiple calls to
            ' Thread.Sleep.
            If Not counted Then
                numWaits += 1
                counted = True
            End If
        End While

        ' Any processing done after this point does not conflict
        ' with timer events. This is the purpose of the call to
        ' CompareExchange. If the processing done here would not
        ' cause a problem when run concurrently with timer events,
        ' then there is no need for the extra synchronization.
    End Sub

    ' Event-handling methods for the Elapsed events of the two
    ' timers.
    Private Sub Timer1_ElapsedEventHandler(ByVal sender As Object, _
        ByVal e As ElapsedEventArgs) Handles Timer1.Elapsed

        HandleElapsed(sender, e)
    End Sub

    Private Sub Timer2_ElapsedEventHandler(ByVal sender As Object, _
        ByVal e As ElapsedEventArgs) Handles Timer2.Elapsed

        HandleElapsed(sender, e)
    End Sub

    Private Sub HandleElapsed(ByVal sender As Object, ByVal e As ElapsedEventArgs)

        numEvents += 1

        ' This example assumes that overlapping events can be
        ' discarded. That is, if an Elapsed event is raised before 
        ' the previous event is finished processing, the second
        ' event is ignored. 
        ' CompareExchange is used to take control of syncPoint, 
        ' and to determine whether the attempt was successful. 
        ' CompareExchange attempts to put 1 into syncPoint, but
        ' only if the current value of syncPoint is zero 
        ' (specified by the third parameter). If another thread
        ' has set syncPoint to 1, or if the control thread has
        ' set syncPoint to -1, the current event is skipped. 
        ' (Normally it would not be necessary to use a local 
        ' variable for the return value. A local variable is 
        ' used here to determine the reason the event was 
        ' skipped.)
        Dim sync As Integer = Interlocked.CompareExchange(syncPoint, 1, 0)
        If sync = 0 Then
            ' No other event was executing.
            ' The event handler simulates an amount of work
            ' similar to the time between events, so that
            ' some events will overlap.
            Dim delay As Integer = timerIntervalBase _
                - timerIntervalDelta / 2 + rand.Next(timerIntervalDelta)
            numExecuted += 1

            ' Release control of syncPoint.
            syncPoint = 0
            If sync = 1 Then numSkipped += 1 Else numLate += 1
        End If
    End Sub 

End Module

' On a dual-processor computer, this code example produces
' results similar to the following:
'Test 100/100    100 test runs completed.
'436 events were raised.
'352 events executed.
'84 events were skipped for concurrency.
'0 events were skipped because they were late.
'Control thread waited 77 times for an event to complete.

.NET Framework
Available since 1.1
Return to top
© 2016 Microsoft