Usuário com melhor resposta
Funcionamento do Timer

Pergunta
-
Eu estou usando o timer System.Timers.Timer e estou em dúvida quanto ao funcionamento dele.
Pela minha lógica eu tenho a propriedade meuTimer.AutoReset = false e depois que eu executo uma determinada ação, aciono com timer para dar um tempo até a próxima ação.
O problema é que antes de cada ação eu estava modificando a propriedade Interval e cada vez que eu modificava ela o timer estourava e isso fazia ele executar a ação duas vezes seguidas.
Resolvi o problema criando dois timers com tempos diferentes.
Mas a minha dúvida é se devia acontecer mesmo isso, de ocorrer o evendo do timer elapsed quando mudar a propriedade interval.
Respostas
-
Vinicius,
Você deu uma olhada na documentação?
http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx
If the interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.
If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses. Enabled is then set to false.
If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. To set the interval without raising the event, you can temporarily set the AutoReset property to true.
André Alves de Lima
Visite o meu site: http://andrealveslima.spaces.live.com
Me siga no Twitter: @andrealveslima- Marcado como Resposta Vinicius Hoff terça-feira, 28 de setembro de 2010 16:06
Todas as Respostas
-
Vinícius,
Se você modifica o Interval de dentro do evento, ele começa de novo, mas se o problema é somente dar uma pausa entre um evento e outro, por quê você não usa:
System.Threading.
Thread.Sleep(1000);
[]s,
Carlos dos Santos - www.carloscds.net- Sugerido como Resposta Rafael Tomitão terça-feira, 28 de setembro de 2010 02:31
-
Mas eu não estava modificando o Interval de dentro do evento, estava modificando ele em outro método, antes de iniciar o Timer, e mesmo assim ele era chamado.
Não sei se usar o Thread.Sleep() me ajudaria, porque tem outras coisas que essa classe faz durante esse tempo, até poderia usar, mas ia precisar mudar mais coisas. De qualquer jeito ele tá funcionando com o uso de dois timers diferentes.
Mas ainda queria saber porque o timer começa quando mudo o Interval dele.
-
-
Vinicius,
Você deu uma olhada na documentação?
http://msdn.microsoft.com/en-us/library/system.timers.timer.interval.aspx
If the interval is set after the Timer has started, the count is reset. For example, if you set the interval to 5 seconds and then set the Enabled property to true, the count starts at the time Enabled is set. If you reset the interval to 10 seconds when count is 3 seconds, the Elapsed event is raised for the first time 13 seconds after Enabled was set to true.
If Enabled is set to true and AutoReset is set to false, the Timer raises the Elapsed event only once, the first time the interval elapses. Enabled is then set to false.
If Enabled and AutoReset are both set to false, and the timer has previously been enabled, setting the Interval property causes the Elapsed event to be raised once, as if the Enabled property had been set to true. To set the interval without raising the event, you can temporarily set the AutoReset property to true.
André Alves de Lima
Visite o meu site: http://andrealveslima.spaces.live.com
Me siga no Twitter: @andrealveslima- Marcado como Resposta Vinicius Hoff terça-feira, 28 de setembro de 2010 16:06
-