I\'m using the System.Timers.Timer class to create a timer with an Timer.Elapsed event. The thing is the Timer.Elapsed event is fired
If you want to be able to raise the event whenever you want (not only just at the moment you start the timer), you can encapsulate a timer in your own MyTimer class. This class exposes the original Timer methods and properties. Furthermore I added an event with explicit add and remove. In this way whenever you add a delegate to the event this is added to both the private MyTimer's event and to the original timer Elapsed event. This means that the timer triggers Elapsed in the usual way, but you can manually trigger the event calling RaiseElapsed (this should sound much simpler looking at the code).
public class MyTimer
{
Timer t = new Timer();
event ElapsedEventHandler timerElapsed;
public event ElapsedEventHandler Elapsed
{
add
{
t.Elapsed += value;
timerElapsed += value;
}
remove
{
t.Elapsed -= value;
timerElapsed -= value;
}
}
public double Interval
{
get
{
return t.Interval;
}
set
{
t.Interval = value;
}
}
public void Start()
{
t.Start();
}
public void Stop()
{
t.Stop();
}
public void RaiseElapsed()
{
if (timerElapsed != null)
timerElapsed(null, null);
}
}