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
  1. Home
  2. General Programming
  3. C#
  4. timer problem

timer problem

Scheduled Pinned Locked Moved C#
adobehelpquestion
10 Posts 8 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.
  • N Offline
    N Offline
    nlowdon
    wrote on last edited by
    #1

    Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

    public Form1()
    {
    InitializeComponent();

            buttonTimer = new Timer();
            buttonTimer.Tick += new EventHandler(button\_Tick);
            buttonTimer.Interval = (50);
            
            populate.BackColor = Color.Black;
            populate.ForeColor = Color.Orange;
    
            buttonTimer.Start();
        }
    
        void button\_Tick(object sender, EventArgs e)
        {
            if (populate.BackColor == Color.Orange)
            {
                populate.BackColor = Color.Black;
                populate.ForeColor = Color.Orange;
            }
    
            if (populate.BackColor == Color.Black)
            {
                populate.BackColor = Color.Orange;
                populate.ForeColor = Color.Black;
            }
        }
    

    }

    K G L R 5 Replies Last reply
    0
    • N nlowdon

      Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

      public Form1()
      {
      InitializeComponent();

              buttonTimer = new Timer();
              buttonTimer.Tick += new EventHandler(button\_Tick);
              buttonTimer.Interval = (50);
              
              populate.BackColor = Color.Black;
              populate.ForeColor = Color.Orange;
      
              buttonTimer.Start();
          }
      
          void button\_Tick(object sender, EventArgs e)
          {
              if (populate.BackColor == Color.Orange)
              {
                  populate.BackColor = Color.Black;
                  populate.ForeColor = Color.Orange;
              }
      
              if (populate.BackColor == Color.Black)
              {
                  populate.BackColor = Color.Orange;
                  populate.ForeColor = Color.Black;
              }
          }
      

      }

      K Offline
      K Offline
      Kristian Sixhoj
      wrote on last edited by
      #2

      Instead of this:

      buttonTimer.Tick += new EventHandler(button_Tick);

      Try this:

      buttonTimer.Elapsed += new EventHandler(button_Elapsed);

      Don't know if it makes any difference, but it's worth a try.

      Kristian Sixhoej sixhoej.net - forums.sixhoej.net

      G 1 Reply Last reply
      0
      • N nlowdon

        Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

        public Form1()
        {
        InitializeComponent();

                buttonTimer = new Timer();
                buttonTimer.Tick += new EventHandler(button\_Tick);
                buttonTimer.Interval = (50);
                
                populate.BackColor = Color.Black;
                populate.ForeColor = Color.Orange;
        
                buttonTimer.Start();
            }
        
            void button\_Tick(object sender, EventArgs e)
            {
                if (populate.BackColor == Color.Orange)
                {
                    populate.BackColor = Color.Black;
                    populate.ForeColor = Color.Orange;
                }
        
                if (populate.BackColor == Color.Black)
                {
                    populate.BackColor = Color.Orange;
                    populate.ForeColor = Color.Black;
                }
            }
        

        }

        G Offline
        G Offline
        Giorgi Dalakishvili
        wrote on last edited by
        #3

        Timer.AutoReset[^]

        Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion

        1 Reply Last reply
        0
        • N nlowdon

          Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

          public Form1()
          {
          InitializeComponent();

                  buttonTimer = new Timer();
                  buttonTimer.Tick += new EventHandler(button\_Tick);
                  buttonTimer.Interval = (50);
                  
                  populate.BackColor = Color.Black;
                  populate.ForeColor = Color.Orange;
          
                  buttonTimer.Start();
              }
          
              void button\_Tick(object sender, EventArgs e)
              {
                  if (populate.BackColor == Color.Orange)
                  {
                      populate.BackColor = Color.Black;
                      populate.ForeColor = Color.Orange;
                  }
          
                  if (populate.BackColor == Color.Black)
                  {
                      populate.BackColor = Color.Orange;
                      populate.ForeColor = Color.Black;
                  }
              }
          

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I had this problem, the cause is that you are setting your event in the load method, which exits so the event is no longer registered. buttonTimer.Tick += new EventHandler(button_Tick); Put that in another method so that it is set again. The way I explained this is probably not the most tech savvy way, but I'm 100% sure that your event gets triggered only once because the form load method quits after your form is loaded.

          P 1 Reply Last reply
          0
          • N nlowdon

            Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

            public Form1()
            {
            InitializeComponent();

                    buttonTimer = new Timer();
                    buttonTimer.Tick += new EventHandler(button\_Tick);
                    buttonTimer.Interval = (50);
                    
                    populate.BackColor = Color.Black;
                    populate.ForeColor = Color.Orange;
            
                    buttonTimer.Start();
                }
            
                void button\_Tick(object sender, EventArgs e)
                {
                    if (populate.BackColor == Color.Orange)
                    {
                        populate.BackColor = Color.Black;
                        populate.ForeColor = Color.Orange;
                    }
            
                    if (populate.BackColor == Color.Black)
                    {
                        populate.BackColor = Color.Orange;
                        populate.ForeColor = Color.Black;
                    }
                }
            

            }

            R Offline
            R Offline
            ricmil42
            wrote on last edited by
            #5

            Looks like you are testing the Color for Orange and if so, setting it to Black. Right after that you test for Black and set it back to Orange. After the ForeColor = Color.Orange, add a return ;.

            D G 2 Replies Last reply
            0
            • R ricmil42

              Looks like you are testing the Color for Orange and if so, setting it to Black. Right after that you test for Black and set it back to Orange. After the ForeColor = Color.Orange, add a return ;.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Bingo! This is just what I was thinking.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008

              1 Reply Last reply
              0
              • N nlowdon

                Evening all ! I've created a new timer in a form's constructor to make a button on the form flash. For some reason the program is stepping through the code for the timer tick ONCE and thats it ? Would appreciate if someone could take a look at it and tell me whats wrong, it's porbably something trivial and i've just been looking at the code too long now ! Thanks in advance Neil

                public Form1()
                {
                InitializeComponent();

                        buttonTimer = new Timer();
                        buttonTimer.Tick += new EventHandler(button\_Tick);
                        buttonTimer.Interval = (50);
                        
                        populate.BackColor = Color.Black;
                        populate.ForeColor = Color.Orange;
                
                        buttonTimer.Start();
                    }
                
                    void button\_Tick(object sender, EventArgs e)
                    {
                        if (populate.BackColor == Color.Orange)
                        {
                            populate.BackColor = Color.Black;
                            populate.ForeColor = Color.Orange;
                        }
                
                        if (populate.BackColor == Color.Black)
                        {
                            populate.BackColor = Color.Orange;
                            populate.ForeColor = Color.Black;
                        }
                    }
                

                }

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                it isn't stepping through once - stick a breakpoint in to prove that. it just has no effect the 2nd and subsequent times... replace

                if (populate.BackColor == Color.Black)

                with "else" and all will be well. Inidentally, if I were you, I would put ((Timer)sender).Stop(); at the start of yor buttin_Click event handler, and ((Timer)sender).Start(); at the end. This stops the timer triggering while the code is still executing - and helps when you are debugging too!

                If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                1 Reply Last reply
                0
                • L Lost User

                  I had this problem, the cause is that you are setting your event in the load method, which exits so the event is no longer registered. buttonTimer.Tick += new EventHandler(button_Tick); Put that in another method so that it is set again. The way I explained this is probably not the most tech savvy way, but I'm 100% sure that your event gets triggered only once because the form load method quits after your form is loaded.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  That makes no sense at all.

                  1 Reply Last reply
                  0
                  • K Kristian Sixhoj

                    Instead of this:

                    buttonTimer.Tick += new EventHandler(button_Tick);

                    Try this:

                    buttonTimer.Elapsed += new EventHandler(button_Elapsed);

                    Don't know if it makes any difference, but it's worth a try.

                    Kristian Sixhoej sixhoej.net - forums.sixhoej.net

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    That's for a System.Timer, not a System.Windows.Forms.Timer. The Elapsed event is run in a separate thread, so that can't be used to update controls in the form.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    • R ricmil42

                      Looks like you are testing the Color for Orange and if so, setting it to Black. Right after that you test for Black and set it back to Orange. After the ForeColor = Color.Orange, add a return ;.

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      Better yet, use an else:

                      if (populate.BackColor == Color.Orange) {
                      populate.BackColor = Color.Black;
                      populate.ForeColor = Color.Orange;
                      } else {
                      populate.BackColor = Color.Orange;
                      populate.ForeColor = Color.Black;
                      }

                      Despite everything, the person most likely to be fooling you next is yourself.

                      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