Single timer_tick() for multiple timers, how to get the sender

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nunu21
    New Member
    • Oct 2008
    • 11

    Single timer_tick() for multiple timers, how to get the sender

    How do I know which Timer_Tick which timer object has raised the tick event?
    Please refer some code.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You plan to use the same _tick() function for multiple timers?
    What is the point of that?

    At any rate, the "object sender" parameter that gets passed in, will tell you which object triggered the event.

    Comment

    • nunu21
      New Member
      • Oct 2008
      • 11

      #3
      Originally posted by Plater
      You plan to use the same _tick() function for multiple timers?
      What is the point of that?

      At any rate, the "object sender" parameter that gets passed in, will tell you which object triggered the event.

      yes i plan to use the same_tick() function.
      can u please send me some code to fetch that object and index of timer array using "object sender" parameter.

      Comment

      • nunu21
        New Member
        • Oct 2008
        • 11

        #4
        yes i plan to use the same_tick() function.
        can u please send me some code to fetch that object and index of timer array using "object sender" parameter.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          Once again, it doesn't make sense to use the same handler, but the "sender" parameter is a reference to the object that fired the event.

          So you'd have to do something like
          Code:
          if(sender == timer1)
            something;
          else if(sender == timer2)
            somethingelse;
          Or in your case you can loop through the array, comparing each iteration to sender.

          Comment

          • nunu21
            New Member
            • Oct 2008
            • 11

            #6
            actually i am creating a array of System.Timers.t imer. So it is nothing basically a one timer.
            like timer[0];
            timer[1] like this.

            So i want to know which timer array has raised the tick event.

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Like I said:
              Originally posted by insertAlias
              Or in your case you can loop through the array, comparing each iteration to sender.

              Comment

              • nunu21
                New Member
                • Oct 2008
                • 11

                #8
                i have created a timer array like this:
                Code:
                for (int i = 0; i < totalNumberOfThreads; i++)
                                {
                                tmrThread[totalNumberOfThreads] = new System.Timers.Timer();
                             
                                tmrThread[totalNumberOfThreads].Enabled = true;
                                tmrThread[totalNumberOfThreads].Interval = 120000;
                                tmrThread[totalNumberOfThreads].Elapsed +=new System.Timers.ElapsedEventHandler(timer1_Tick);
                }
                
                private void timer1_Tick(object sender, System.Timers.ElapsedEventArgs e)
                        {
                           
                            if (workerThreads[iIndex].IsAlive)
                            {
                                workerThreads[iIndex].Abort();
                                workerThreads[iIndex].Start();
                            }
                            
                        }
                in this case hoq could i know which timer object has raised the tick event. and then i want the index of that timer array.using that index i will start the thread.

                Please help me out.
                Last edited by Curtis Rutland; Oct 14 '08, 01:21 PM. Reason: added code tags

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Use the sender object like we said:
                  [code=c#]
                  for (int i = 0; i < tmrThread.Lengt h; i++)
                  {
                  if (sender==tmrThr ead[i])
                  {//this is the one that sent the request
                  }
                  }
                  [/code]

                  Comment

                  • nunu21
                    New Member
                    • Oct 2008
                    • 11

                    #10
                    i am doing this

                    Code:
                    for (int i = 0; i < tmrThread.Length; i++)
                                {
                                    if (sender == tmrThread[i])
                                    {
                                        workerThreads[i].Abort();
                                        workerThreads[i].Start();
                                    }
                                }
                    but it's not working.
                    Last edited by Curtis Rutland; Oct 14 '08, 01:21 PM. Reason: added code tags

                    Comment

                    • Curtis Rutland
                      Recognized Expert Specialist
                      • Apr 2008
                      • 3264

                      #11
                      Please use [CODE] tags when you post code. It makes it far easier for us to read, and the easier it is for us, the more likely you are to get good help.

                      MODERATOR

                      Comment

                      • nunu21
                        New Member
                        • Oct 2008
                        • 11

                        #12
                        soryy but it's not working.

                        Comment

                        • nunu21
                          New Member
                          • Oct 2008
                          • 11

                          #13
                          In sender value is 'System.Timers. Timer' and in tmrThread[i] the value is different.

                          How it is going to check?

                          Comment

                          • Plater
                            Recognized Expert Expert
                            • Apr 2007
                            • 7872

                            #14
                            tmrThread[i] is an isntance of System.Timers.T imer, the sender object is a System.Timer.Ti mer, this should not be a difficult concept?
                            Or did I miss something?

                            Its not any different then assigning a single buttonclick eventhandler to multiple buttons.

                            Comment

                            Working...