Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multiple Timers in a program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Multiple Timers in a program

    Hi,

    Can you let me know how I can have more multiple timers in a program

    I have for example the code below How can I run one set of code if Timer1 is called and another for Timer?

    timer1.Elapsed += OnTimedEvent; //Wait a period beforestatring again
    timer1.Start();

    timer.Elapsed += OnTimedEvent; //Wait a period beforestatring again
    timer.Start();


    private void OnTimedEvent(Object source, ElapsedEventArgs e)

    {


    }

    #2
    Originally posted by cocopod View Post
    Hi,

    Can you let me know how I can have more multiple timers in a program

    I have for example the code below How can I run one set of code if Timer1 is called and another for Timer?

    timer1.Elapsed += OnTimedEvent; //Wait a period beforestatring again
    timer1.Start();

    timer.Elapsed += OnTimedEvent; //Wait a period beforestatring again
    timer.Start();


    private void OnTimedEvent(Object source, ElapsedEventArgs e)

    {


    }
    ? Query the source of the event?

    Comment


      #3
      Hello,

      You can make as many timers as you want, where the timer is pointed to for its elapsed method will determine the outcome.

      In your example:
      Originally posted by cocopod View Post
      Hi,
      timer1.Elapsed += OnTimedEvent; //Wait a period beforestatring again
      timer1.Start();

      timer.Elapsed += OnTimedEvent; //Wait a period beforestatring again
      timer.Start();


      private void OnTimedEvent(Object source, ElapsedEventArgs e)

      {


      }
      you have two timers that share a common event handler or OnTimedEvent.

      To have two separate codes run in two different events you would need to add one more event callback to allow this.
      the following would work for two separate timers.

      Code:
      timer.Elapsed += OnTimedEvent; //Wait a period beforestatring again
      timer.Start();
      private void OnTimedEvent(Object source, ElapsedEventArgs e)
      {
      //timer elapsed
      }
      
      timer1.Elapsed += OnTimedEvent1; //Wait a period beforestatring again
      timer1.Start();
      private void OnTimedEvent1(Object source, ElapsedEventArgs e)
      {
      //timer1 elapsed
      }
      the method OnTimedEvent1 is created to handle your second timer. two timers can share a event handler or they can each have their own.

      Here is more information regarding Timers: http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx

      When you get into using basic C# components such as a timer the MSDN reference will be your #1 source for information and examples. This is where I go for any C# reference information in case I need an example or need to read how to use it.

      Please let me know if I may be of additional assistance.

      Comment


        #4
        Thanks Jesse,

        guess you probably hear this all the time but I actually tried that and it didn't appear to work...But now amazingly it does.

        anyway onwards and upwards

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        648 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        369 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        108 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        572 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X