Internal of .NET timer controller

TRiNE

In VB.NET or C#, windows form applications can be added a timer controller to run tasks in scheduled manner. But what is the internal implementation of a timer? Does it use a separate thread for each timer? or Thread pool? or other concurrent programming paradigm?

EDIT

Additionally, I want to know does the timer controller satisfy concurrent safty property as described below?

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    i = i + 1 //Line 1
              //Line 2, some code is here will takes some cpu time
    i = i - 1 //Line 3
              //Line 4, some code is here; takes another cpu time
End Sub

Can we guarantee that public variable i always have the values 1 or 0?

Krumelur

The Windows Forms timer (System.Windows.Forms.Timer) will run on the UI thread (it will post timer events to the main event loop).

The System.Timers.Timer will by default use the thread pool for timer events.

To answer your updated question: Assuming you are using the System.Windows.Forms.Timer, the tick handler will always run to completion before the next tick event is run (since they always run on the same thread). Don't forget to wrap in try/finally though.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related