Torna al Thread

/// <summary> /// Fornisce un semplice Timer /// </summary> public class Timer { private System.Threading.Timer timer = null; public System.Threading.TimerCallback timerCallback = null; private int secondi; private bool _isStarted = false; public bool IsStarted { get { return _isStarted; } } /// <summary> /// Istanzia un nuovo timer con i parametri specificati /// </summary> /// <param name="secondi">Numero di secondi dopo il quale attivare l'evento timerCallBack</param> /// <param name="timerCallback">La funzione da eseguire allo scadere del tempo</param> public Timer(int secondi) { this.secondi = secondi; } /// <summary> /// Avvia il timer in un nuovo thread /// </summary> public void Start() { this.timer = new System.Threading.Timer(this.timerCallback, null, 1000 * secondi, System.Threading.Timeout.Infinite); this._isStarted = true; } /// <summary> /// Riavvia il timer /// </summary> public void Restart() { this.timer.Dispose(); this.timer = new System.Threading.Timer(this.timerCallback, null, 1000 * secondi, System.Threading.Timeout.Infinite); this._isStarted = true; } /// <summary> /// Fermaa il timer /// </summary> public void Stop() { if (timer != null) { this.timer.Dispose(); this._isStarted = false; } } public void Dispose() { if (timer != null) timer.Dispose(); } }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5