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 charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        64 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        149 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        99 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        286 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X