Usuário com melhor resposta
Tempo restante de um timer

Pergunta
-
Olá a todos,
Alguém sabe se é possível o timer retornar quanto tempo falta para ele ser executado? Por exemplo criei o seguinte timer
System.Windows.Threading.DispatcherTimer timerSMS = new System.Windows.Threading.DispatcherTimer(); timerSMS.Tick += new EventHandler(timerSMS_Tick); timerSMS.Interval = new TimeSpan(2, 30, 0); timerSMS.Start();
Queria saber se tem alguma propriedade que poderia fazer algo tipo isso
//esse timerLeft não existe, existe algo do tipo??? TimeSpan tempoRestante = timerSMS.timeLeft;
Att,
Gustavo Freitas
Respostas
-
A biblioteca nao forcence algo para isto, mas voce pode criar a seguinte classe para resolver o problema:
public class TimerPlus : IDisposable { private readonly TimerCallback _realCallback; private readonly Timer _timer; private TimeSpan _period; private DateTime _next; public TimerPlus(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) { _timer = new Timer(Callback, state, dueTime, period); _realCallback = callback; _period = period; _next = DateTime.Now.Add(dueTime); } private void Callback(object state) { _next = DateTime.Now.Add(_period); _realCallback(state); } public TimeSpan Period { get { return _period; } } public DateTime Next { get { return _next; } } public TimeSpan DueTime { get { return _next - DateTime.Now; } } public bool Change(TimeSpan dueTime, TimeSpan period) { _period = period; _next = DateTime.Now.Add(dueTime); return _timer.Change(dueTime, period); } public void Dispose() { _timer.Dispose(); } }
(fonte = http://stackoverflow.com/questions/2278525/system-timers-timer-how-to-get-the-time-remaining-until-elapse)
Por favor, lembre-se de “Marcar como Resposta” as respostas que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.- Editado Erick WendelMVP terça-feira, 12 de janeiro de 2016 12:26
- Sugerido como Resposta Erick WendelMVP terça-feira, 12 de janeiro de 2016 13:11
- Marcado como Resposta Gustavo_Freitas terça-feira, 12 de janeiro de 2016 18:53
Todas as Respostas
-
A biblioteca nao forcence algo para isto, mas voce pode criar a seguinte classe para resolver o problema:
public class TimerPlus : IDisposable { private readonly TimerCallback _realCallback; private readonly Timer _timer; private TimeSpan _period; private DateTime _next; public TimerPlus(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) { _timer = new Timer(Callback, state, dueTime, period); _realCallback = callback; _period = period; _next = DateTime.Now.Add(dueTime); } private void Callback(object state) { _next = DateTime.Now.Add(_period); _realCallback(state); } public TimeSpan Period { get { return _period; } } public DateTime Next { get { return _next; } } public TimeSpan DueTime { get { return _next - DateTime.Now; } } public bool Change(TimeSpan dueTime, TimeSpan period) { _period = period; _next = DateTime.Now.Add(dueTime); return _timer.Change(dueTime, period); } public void Dispose() { _timer.Dispose(); } }
(fonte = http://stackoverflow.com/questions/2278525/system-timers-timer-how-to-get-the-time-remaining-until-elapse)
Por favor, lembre-se de “Marcar como Resposta” as respostas que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.- Editado Erick WendelMVP terça-feira, 12 de janeiro de 2016 12:26
- Sugerido como Resposta Erick WendelMVP terça-feira, 12 de janeiro de 2016 13:11
- Marcado como Resposta Gustavo_Freitas terça-feira, 12 de janeiro de 2016 18:53
-
A biblioteca nao forcence algo para isto, mas voce pode criar a seguinte classe para resolver o problema:
public class TimerPlus : IDisposable { private readonly TimerCallback _realCallback; private readonly Timer _timer; private TimeSpan _period; private DateTime _next; public TimerPlus(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period) { _timer = new Timer(Callback, state, dueTime, period); _realCallback = callback; _period = period; _next = DateTime.Now.Add(dueTime); } private void Callback(object state) { _next = DateTime.Now.Add(_period); _realCallback(state); } public TimeSpan Period { get { return _period; } } public DateTime Next { get { return _next; } } public TimeSpan DueTime { get { return _next - DateTime.Now; } } public bool Change(TimeSpan dueTime, TimeSpan period) { _period = period; _next = DateTime.Now.Add(dueTime); return _timer.Change(dueTime, period); } public void Dispose() { _timer.Dispose(); } }
(fonte = http://stackoverflow.com/questions/2278525/system-timers-timer-how-to-get-the-time-remaining-until-elapse)
Por favor, lembre-se de “Marcar como Resposta” as respostas que resolveram o seu problema. Essa é uma maneira comum de reconhecer aqueles que o ajudaram e fazer com que seja mais fácil para os outros visitantes encontrarem a resolução mais tarde.
Para usar essa classe seria dessa maneira?
private void Window_Loaded(object sender, RoutedEventArgs e) { AutoResetEvent autoEvent = new AutoResetEvent(false); TimerCallback timerDelegate = new TimerCallback(timer_Elapsed); TimeSpan delayTime = new TimeSpan(0, 0, 0); TimeSpan intervalTime = new TimeSpan(0, 0, 5); stateTimer = new TimerPlus(timerDelegate, autoEvent, delayTime, intervalTime); } void timer_Elapsed(Object stateInfo) { //Codigo Aqui }