I have a doubt with my timer, when I start it with a button it executes my function as I ask but the problem comes when I try to stop it with the timer.stop() method, since it continues to execute in the timer.elapsed section , here the code
I already tried it with timer.dispose(), timer.stop(), timer.Enabled = false
Define your timer variable at the class level so that you are not creating another instance when starting/stopping the timer. This way by changing the value of flag when calling the method again, you will be able to stop/start the timer without problem.
Even so that the call to "test()" is not repeated, you must initialize timer.Elapsed elsewhere, example:
It would be useful to know who and how the code that appears in the image is being called, with the current context it is difficult to understand what is happening.
What I see at first glance is that you are creating a new instance of the timer each time that code is executed, so when it does the it
timer.Stop()
is using a different instance than the one you added the event to withtimer.Elapsed
. That is, you are stopping another timer. You should instantiate the timer in a higher scope that can be reused. For example: