How to tell if a System.Timers.Timer is running? [closed]

0

I need to know if a timer is running, if it is not, I will start it,

    
asked by anonymous 15.07.2016 / 02:39

1 answer

2

The Timer class does not has nothing to control it. If you want to prevent a specific timer from being created again you need to create your own flag control indicating that it has started and will not start again.

In fact the problem seems different. This timer can be destroyed when the runtime wants. It is being declared locally in the method, when the method is over, the object is free to be destroyed by the garbage collector at some point . So doing this is a mistake.

If the intention was that it should be destroyed then you would need to ensure that this happens immediately with a using . . I do not think that's the case.

If the intent is to keep the timer alive after the method finishes, and it seems to be this, it should be placed in a class instance variable to last the entire existence of the form, or even a static class variable if the goal is to keep alive in every application. If it is created inside the class then you have to take care of destroying the timer when the object that contains it is destroyed (the class will need disposable and it will have to be created properly for this.

You even need to think about whether this is the right fit for what you want. I've seen a lot of misuse of it. If it is, you need to understand all of its implications.

    
15.07.2016 / 03:12