Reputation: 2492
I have simple timer:
private System.Timers.Timer _timer;
public Ctor()
{
_timer = new System.Timers.Timer(timeout);
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}
How to make that timer rise Elapsed
event after timeout
time expired?
It is for the first elapsed event, of course.
Upvotes: 0
Views: 1859
Reputation: 2912
I think you're asking for a timer to be started after an initial delay? If that's the case then consider using a System.Threading.Timers.Timer
instead, as follows:
int initialDelay = 5000; // five seconds
int timerPeriod = 1000; // one second
var timer = new Timer(_ => MethodToCallWhenExpired(), null, initialDelay, timerPeriod);
Alternatively you can use Timeout.Infinite
and then call the Change()
method to alter the Timer behaviour after creation.
Sorry if that's not what you were asking though! :)
Upvotes: 0
Reputation: 30052
A Timer does exactly what you want , it raises the event after the specified interval elapses. If you don't want it to be recurring (Runs once only), then set the AutoResetProperty
:
public Ctor()
{
_timer = new System.Timers.Timer(timeout);
_timer.AutoReset = false;
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
// your code here runs after the timeout elapsed
}
Upvotes: 3