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. create Timers dynamically

create Timers dynamically

Scheduled Pinned Locked Moved C#
databasehelp
10 Posts 4 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.
  • U Offline
    U Offline
    User 3055467
    wrote on last edited by
    #1

    Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance

    M D 2 Replies Last reply
    0
    • U User 3055467

      Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      Hmmm.. i have a feeling your going about this the wrong way... What are you trying to do with each timer? There is nothing in your example code that shows you doing anything with the instance of a timer that would need it to be identified

      Life goes very fast. Tomorrow, today is already yesterday.

      U 1 Reply Last reply
      0
      • U User 3055467

        Hi, I am working in windows application in which I need to create Timers dynamically based on number of records in database. I am able to do that but the problem is I can't identify which timer is raising the tick event. Below is the sample code on which I am working. public Form1() { InitializeComponent(); System.Timers.Timer timer1 = new System.Timers.Timer; timer1.Enabled = true; timer1.Interval = 5000; timer1.Elapsed += new ElapsedEventHandler(TimerTick); timer1.Start(); } public void TimerTick(object source, ElapsedEventArgs e) { string s = ((System.Timers.Timer)source); string s1 = e.ToString(); } Thanks in advance

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        You can cast to a Timer from the sender

        if(sender is System.Timers.Timer)
        {
        System.Timers.Timer thisTimer = sender as System.Timers.Timer;
        }

        If you want you can give it a property such as an int for ID or a string for Name etc by subclassing e.g.

        public class MyTimer : System.Timers.Timer
        {
        public string Name
        {
        get;
        set;
        }
        }

        ... and then use MyTimer instead of Timer.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        U 1 Reply Last reply
        0
        • M musefan

          Hmmm.. i have a feeling your going about this the wrong way... What are you trying to do with each timer? There is nothing in your example code that shows you doing anything with the instance of a timer that would need it to be identified

          Life goes very fast. Tomorrow, today is already yesterday.

          U Offline
          U Offline
          User 3055467
          wrote on last edited by
          #4

          Hi Thanks a lot for reoplying so soon I cant tell the requirement completely. Let me explain in brief.In database I have a set of records in this structure. ID ConsumerID Time Interval 1 5 5 2 6 7 3 8 9 Now I have 3 records in database. I need to create 3 timers and set intervals accordingly. For every 5 secs I should get details regarding ConsumerID-5 For every 7 secs I should get details regarding ConsumerID-6 For every 9 secs I should get details regarding ConsumerID-8 Thanks

          M 1 Reply Last reply
          0
          • U User 3055467

            Hi Thanks a lot for reoplying so soon I cant tell the requirement completely. Let me explain in brief.In database I have a set of records in this structure. ID ConsumerID Time Interval 1 5 5 2 6 7 3 8 9 Now I have 3 records in database. I need to create 3 timers and set intervals accordingly. For every 5 secs I should get details regarding ConsumerID-5 For every 7 secs I should get details regarding ConsumerID-6 For every 9 secs I should get details regarding ConsumerID-8 Thanks

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            Ok well you could go with Davey's idea of inheritance or you could use the timer's 'Tag' property as follows... when creating...

            Timer t = new Timer();
            //add event handlers and what not
            t.Tag = contactID;

            then in event handler...

            Timer t = (Timer)sender;
            if(t.Tag != null)
            {
            string contactID = (string)t.Tag;
            //then do some DB stuff to get the contact details use contactID
            }

            Life goes very fast. Tomorrow, today is already yesterday.

            D 1 Reply Last reply
            0
            • M musefan

              Ok well you could go with Davey's idea of inheritance or you could use the timer's 'Tag' property as follows... when creating...

              Timer t = new Timer();
              //add event handlers and what not
              t.Tag = contactID;

              then in event handler...

              Timer t = (Timer)sender;
              if(t.Tag != null)
              {
              string contactID = (string)t.Tag;
              //then do some DB stuff to get the contact details use contactID
              }

              Life goes very fast. Tomorrow, today is already yesterday.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              The System.Timers.Timer doesn't have a Tag property - only System.Windows.Forms.Timer - an int ID property for each MyTimer : System.Timers.Timer would be the easiest way to go here.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              M 1 Reply Last reply
              0
              • D DaveyM69

                The System.Timers.Timer doesn't have a Tag property - only System.Windows.Forms.Timer - an int ID property for each MyTimer : System.Timers.Timer would be the easiest way to go here.

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #7

                DaveyM69 wrote:

                The System.Timers.Timer doesn't have a Tag property

                My bad :doh: Yeah, in that case I agree with you method

                Life goes very fast. Tomorrow, today is already yesterday.

                1 Reply Last reply
                0
                • D DaveyM69

                  You can cast to a Timer from the sender

                  if(sender is System.Timers.Timer)
                  {
                  System.Timers.Timer thisTimer = sender as System.Timers.Timer;
                  }

                  If you want you can give it a property such as an int for ID or a string for Name etc by subclassing e.g.

                  public class MyTimer : System.Timers.Timer
                  {
                  public string Name
                  {
                  get;
                  set;
                  }
                  }

                  ... and then use MyTimer instead of Timer.

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  U Offline
                  U Offline
                  User 3055467
                  wrote on last edited by
                  #8

                  Thanks a lot Dav I got it by using your method

                  D M 2 Replies Last reply
                  0
                  • U User 3055467

                    Thanks a lot Dav I got it by using your method

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    No problem, you're welcome :-D

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    1 Reply Last reply
                    0
                    • U User 3055467

                      Thanks a lot Dav I got it by using your method

                      M Offline
                      M Offline
                      mell mell
                      wrote on last edited by
                      #10

                      hello sir about timer dynamically .can i copy the code? I do not understand how to do it :confused:

                      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