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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        81 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        42 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        64 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        68 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        55 views
        0 likes
        Last Post CarlTrading  
        Working...
        X