public class TimerObject
{
private Timer timer;
public TimerObject()
{
timer = new Timer();
timer.Tick += new EventHandler(OnTimeElapsed);
timer.Interval = 60000;
}
public event EventHandler Elapsed;
private int _interval = 1;
public int Interval
{
set { _interval = value; }
get { return _interval; }
}
int counter = 0;
private void OnTimeElapsed(object sender,EventArgs e)
{
counter++;
if (Elapsed != null && counter==Interval)
{
Elapsed(sender, e);
counter = 0;
}
}
public bool Enabled
{
set { timer.Enabled = value; }
}
}
调用方法
TimerObject time = new TimerObject();
time.Interval = 1;
time.Elapsed += new EventHandler(time_Elapsed);
time.Enabled = true;
void time_Elapsed(object sender, EventArgs e)
{
MessageBox.Show("dddd");
}