|
-
Feb 2nd, 2006, 04:18 AM
#1
Thread Starter
Lively Member
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
-
Feb 2nd, 2006, 04:31 AM
#2
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?
-
Feb 2nd, 2006, 04:36 AM
#3
Thread Starter
Lively Member
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
-
Feb 2nd, 2006, 04:59 AM
#4
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.
-
Feb 2nd, 2006, 05:12 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|