ArcadiA Club
|
|
|
|
| ||
|
|
||
|
- Timer in Visual Basic .Net -
|
|||
| COSA SERVE PER QUESTO TUTORIAL | |||
| Download | Chiedi sul FORUM | Glossario | cognizioni basiche di un VB .Net | ||
| Qual è il timer migliore in .Net? | |||
CONFRONTO TRA I TRE TIMER DISPONIBILI Le classi timer di System.Threading, System.Windows.Forms e System.Timers. È piuttosto frequente la necessità di eseguire delle azioni a intervalli di tempo ben specificati, e ovviamente quesot in .Net è possibile, tuttavia si potrebbe rimanere inizialmente disorientati dalle possibilità che il framework offre. Esistono infatti ben tre tipi di timer differenti: System.Windows.Forms.Timer, System.Threading.Timer, System.Timers.Timer. Vediamone le caratteristiche:
Brevemente, un ThreadPool è un "bacino" di thread che l'applicazione
ha a disposizione e a cui può attingere; questi thread sono chiamati
worker-thread e svolgono vari compiti di sistema o imposti dall'utente
come in questo caso o ogni volta che si chiamano i metodi asincroni che
iniziano per Begin o End (es. BeginRead, EndConnect e così via).
Public Class frmMain
Private tmrThread As Threading.Timer
Private tmrTimer As System.Timers.Timer
Private tmrWindowsForms As System.Windows.Forms.Timer
Private rnd As New Random()
Const intInterval As Integer = 50
Private Sub frmMain_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
tmrThread = New System.Threading.Timer(AddressOf NewRandomNumber, _
lblSystemThreading, 0, intInterval)
tmrTimer = New System.Timers.Timer(intInterval)
AddHandler tmrTimer.Elapsed, AddressOf tmrTimer_Elapsed
tmrTimer.SynchronizingObject = Me
tmrTimer.Start()
tmrWindowsForms = New System.Windows.Forms.Timer()
tmrWindowsForms.Interval = intInterval
AddHandler tmrWindowsForms.Tick, AddressOf tmrWindowsForms_Tick
tmrWindowsForms.Start()
End Sub
Private Sub frmMain_FormClosed(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Dim ewh As New System.Threading.EventWaitHandle(True, _
Threading.EventResetMode.AutoReset)
tmrThread.Dispose(ewh)
ewh.WaitOne()
tmrWindowsForms.Stop()
tmrTimer.Stop()
End Sub
[...]
End Class
Prima di tutto abbiamo dichiarato i tre tipi di timer, un generatore
di numeri casuali (che verrà usato nella funzione di callback) e una
costante per indicare i millisecondi di intervallo per tutti i timer.
|
|||
| << INDIETRO | by VeNoM00 | ||
| ||||||||||||||
![]() |
||||||||||||||
| ^ TOP | ||||||||||||||
|
pubblicità |
regole |
visione |
diritti |
ufficio stampa |
donazioni |
scrivici |
||||||||||||||