Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to stop a timer from within it's eventhandler?

How to stop a timer from within it's eventhandler?

Scheduled Pinned Locked Moved C#
questiontutorial
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hussam Fattahi
    wrote on last edited by
    #1

    well, i have a timer initilized and started in the Form constructor :

    tmpTimer = new System.Timers.Timer(1000);
    tmpTimer.Elapsed += new ElapsedEventHandler(tmpTimer_Elapsed);
    tmpTimer.Start();
    

    the timer start in the form constructor and keep checking a bool variable(doThing), if this variable is set to true in the program, the timer excute the event handler,

    private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                if (doThing == true )
                {
                    //DoThing Function
                    MessageBox.Show(doThing.ToString());
                }            
            }
    

    but i want the event handler to excute only once(at the time that doThing is set to ture , so i add one line of code to change the value of doThing to false,to prevent excuting of the event handler.

    private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                if (doThing == true )
                {
                    //DoThing Function
                    MessageBox.Show(doThing.ToString());
                    doThing = false;  //To prevent excuting of DoThing Function.
                }
            }
    

    but the poblem it's that the value of doTing dose not changed, and messageboxes keep showing in screen every one second (with True printed on them) till i close one of them, then they stoped showing. what i want is to prevent timer from entring if block by changing doThing to false. i don't know what is realy happened there, any gusse or suggestion would be helpfull, thanks in advance.

    S S 2 Replies Last reply
    0
    • H Hussam Fattahi

      well, i have a timer initilized and started in the Form constructor :

      tmpTimer = new System.Timers.Timer(1000);
      tmpTimer.Elapsed += new ElapsedEventHandler(tmpTimer_Elapsed);
      tmpTimer.Start();
      

      the timer start in the form constructor and keep checking a bool variable(doThing), if this variable is set to true in the program, the timer excute the event handler,

      private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
              {
                  if (doThing == true )
                  {
                      //DoThing Function
                      MessageBox.Show(doThing.ToString());
                  }            
              }
      

      but i want the event handler to excute only once(at the time that doThing is set to ture , so i add one line of code to change the value of doThing to false,to prevent excuting of the event handler.

      private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
              {
                  if (doThing == true )
                  {
                      //DoThing Function
                      MessageBox.Show(doThing.ToString());
                      doThing = false;  //To prevent excuting of DoThing Function.
                  }
              }
      

      but the poblem it's that the value of doTing dose not changed, and messageboxes keep showing in screen every one second (with True printed on them) till i close one of them, then they stoped showing. what i want is to prevent timer from entring if block by changing doThing to false. i don't know what is realy happened there, any gusse or suggestion would be helpfull, thanks in advance.

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      MSDN states: "The Elapsed event is raised on a ThreadPool thread. If the processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread." Since the MessageBox.Show method is a blocking method, doThing is true until you close the first message box, and the execution of the event handler on additional Threadpool threads causes the appearance of a new message box every seconds.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      1 Reply Last reply
      0
      • H Hussam Fattahi

        well, i have a timer initilized and started in the Form constructor :

        tmpTimer = new System.Timers.Timer(1000);
        tmpTimer.Elapsed += new ElapsedEventHandler(tmpTimer_Elapsed);
        tmpTimer.Start();
        

        the timer start in the form constructor and keep checking a bool variable(doThing), if this variable is set to true in the program, the timer excute the event handler,

        private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
                {
                    if (doThing == true )
                    {
                        //DoThing Function
                        MessageBox.Show(doThing.ToString());
                    }            
                }
        

        but i want the event handler to excute only once(at the time that doThing is set to ture , so i add one line of code to change the value of doThing to false,to prevent excuting of the event handler.

        private void tmpTimer_Elapsed(object sender, ElapsedEventArgs e)
                {
                    if (doThing == true )
                    {
                        //DoThing Function
                        MessageBox.Show(doThing.ToString());
                        doThing = false;  //To prevent excuting of DoThing Function.
                    }
                }
        

        but the poblem it's that the value of doTing dose not changed, and messageboxes keep showing in screen every one second (with True printed on them) till i close one of them, then they stoped showing. what i want is to prevent timer from entring if block by changing doThing to false. i don't know what is realy happened there, any gusse or suggestion would be helpfull, thanks in advance.

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        If you move doThing = false above the MessageBox.Show, there will be a small improvement, but there would still be a possibility of multiple message boxes. You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer. Something like

        void StartTimer()
        {
        int dueTime = 5000; // fire in 5 seconds
        object state; // whatever you want to pass to the timer handler
        System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerMethod), state, dueTime, Timeout.Infinite);
        }

        Passing Timeout.Infinite as the last parameter makes the timer run only once.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        S H 2 Replies Last reply
        0
        • S S Senthil Kumar

          If you move doThing = false above the MessageBox.Show, there will be a small improvement, but there would still be a possibility of multiple message boxes. You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer. Something like

          void StartTimer()
          {
          int dueTime = 5000; // fire in 5 seconds
          object state; // whatever you want to pass to the timer handler
          System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerMethod), state, dueTime, Timeout.Infinite);
          }

          Passing Timeout.Infinite as the last parameter makes the timer run only once.

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          S. Senthil Kumar wrote:

          You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer.

          The same can be achieved by setting the System.Timers.Timer.Autoreset property to false.


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          S 1 Reply Last reply
          0
          • S Stefan Troschuetz

            S. Senthil Kumar wrote:

            You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer.

            The same can be achieved by setting the System.Timers.Timer.Autoreset property to false.


            "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

            www.troschuetz.de

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            The documentation[^] on the property doesn't say that the timer will fire only once, it says that the Elapsed event will be raised only once, the first time the timer elapses. You could argue it's the same, but the OP has to write code to stop the timer.

            Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

            1 Reply Last reply
            0
            • S S Senthil Kumar

              If you move doThing = false above the MessageBox.Show, there will be a small improvement, but there would still be a possibility of multiple message boxes. You could try using System.Threading.Timer[^] instead and it provides a parameter to do one shot firing of the timer. Something like

              void StartTimer()
              {
              int dueTime = 5000; // fire in 5 seconds
              object state; // whatever you want to pass to the timer handler
              System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerMethod), state, dueTime, Timeout.Infinite);
              }

              Passing Timeout.Infinite as the last parameter makes the timer run only once.

              Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

              H Offline
              H Offline
              Hussam Fattahi
              wrote on last edited by
              #6

              Thanks alot

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups