none
Winform timer or System.Timers not stop RRS feed

  • Question

  • public Form1()
            {
                InitializeComponent();
    
                timer1 = new System.Timers.Timer();
                timer1.Interval = 1;
                timer1.Elapsed += new ElapsedEventHandler(OnTimedEvent);
                timer1.Enabled = true;
                timer1.AutoReset = true;
    
            }
    
    private void OnTimedEvent(object source, ElapsedEventArgs e)
            {
    
    if(blablabla){
    
    //code
    
    }else{
    
     timer1.Dispose();
    
    timer1.Enabled=false;
    
    }
    
    }
    Not working, please help.
    In the example I showed system.timers, the winform timer does not even stop.


    Monday, April 15, 2019 1:04 AM

Answers

  • First, please place timer1.Enabled = false before timer1.Dispose().

    Changing properties of disposed object will not have any effect (in fact I'm surprised that you don't get a runtime error for doing so)

    Also, try place a breakpoint on blablabla and see whether it's value is "false" when you expect the timer to stop.


    The problem originated from an irrelevant place. I figured it out. Thanks.
    Monday, April 15, 2019 2:16 AM
  • First, please place timer1.Enabled = false before timer1.Dispose().

    Changing properties of disposed object will not have any effect (in fact I'm surprised that you don't get a runtime error for doing so)

    Also, try place a breakpoint on blablabla and see whether it's value is "false" when you expect the timer to stop.

    • Marked as answer by Ulaş Kayalar Monday, April 15, 2019 2:39 PM
    Monday, April 15, 2019 1:14 AM
    Answerer

All replies

  • First, please place timer1.Enabled = false before timer1.Dispose().

    Changing properties of disposed object will not have any effect (in fact I'm surprised that you don't get a runtime error for doing so)

    Also, try place a breakpoint on blablabla and see whether it's value is "false" when you expect the timer to stop.

    • Marked as answer by Ulaş Kayalar Monday, April 15, 2019 2:39 PM
    Monday, April 15, 2019 1:14 AM
    Answerer
  • First, please place timer1.Enabled = false before timer1.Dispose().

    Changing properties of disposed object will not have any effect (in fact I'm surprised that you don't get a runtime error for doing so)

    Also, try place a breakpoint on blablabla and see whether it's value is "false" when you expect the timer to stop.


    The problem originated from an irrelevant place. I figured it out. Thanks.
    Monday, April 15, 2019 2:16 AM
  • First, please place timer1.Enabled = false before timer1.Dispose().

    Changing properties of disposed object will not have any effect (in fact I'm surprised that you don't get a runtime error for doing so)

    Also, try place a breakpoint on blablabla and see whether it's value is "false" when you expect the timer to stop.


    The problem originated from an irrelevant place. I figured it out. Thanks.

    Hi  Ula Kayalar,

    I am glad to know you solved your issue. It would be appreciated if you could close the thread by marking helpful posts as an answer. This will help other members to find the solution quickly if they have faced the similar issue.

    Thank  you for your understanding.


    Best Regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, April 15, 2019 6:35 AM
    Moderator