How would I implement a timer inside a class.
Thanks in advanceCode:public class CTestForATimer
{
public CTestForATimer() // constructor
{
}
}
Printable View
How would I implement a timer inside a class.
Thanks in advanceCode:public class CTestForATimer
{
public CTestForATimer() // constructor
{
}
}
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?
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
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.
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
}
}