Results 1 to 5 of 5

Thread: How To Create A Timer Inside A Class

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    How To Create A Timer Inside A Class

    How would I implement a timer inside a class.


    Code:
    public class CTestForATimer
    {
         public CTestForATimer() // constructor 
         {
    
         }
    }
    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To Create A Timer Inside A Class

    It depends what you mean by a timer. There are Timer classes in the .NET Framework that will raise an event at a pre-defined interval. If you mean something like a stopwatch then it's not too difficult. You don't actually have to do anything except when the user starts, stops, pauses or something else like that. You just need to do some arithmetic with DateTime and TimeSpan objects. Can you elaborate a little?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How To Create A Timer Inside A Class

    I need a timer(several actually) with the same functionality as when you draw one on a form.
    It needs to be able to start and stop.
    I also need it to execute code every XXXX interval

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How To Create A Timer Inside A Class

    You can use a Timer outside of a form. If you're creating a WinForms app then you can still use a Windows.Forms.Timer, otherwise use a Timers.Timer. You just have to create one in code is all. Declare the variable, call the constructor, set the properties, write the Tick/Elapsed event handler and add it to the event's handler collection. If you've added a Timer to a form in the designer then you can look at the auto-generated code to see what it looks like. The code that you write will be pretty much identical.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    121

    Re: How To Create A Timer Inside A Class

    Thanks that got me on the right track.
    I came up with this code in case anyone else needs it

    Code:
    public class CTestForATimer
    {
        Timer tmr = new Timer();
    
        public CTestForATimer()
        {
            tmr.Tick += new System.EventHandler(tmr_Tick);
            tmr.Interval = 1000;
        }
    
        public void startTimer() 
        {
            tmr.Enabled = true;
        }
    
        private void tmr_Tick(object sender, EventArgs e)
        {
            // called every interval
        }
    
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width