Skip to content

Instantly share code, notes, and snippets.

@amitkhare
Last active December 31, 2019 12:11
Show Gist options
  • Save amitkhare/a8736273f743ca8db596f455486150ef to your computer and use it in GitHub Desktop.
Save amitkhare/a8736273f743ca8db596f455486150ef to your computer and use it in GitHub Desktop.
private System.Threading.Timer timer;
private void SetUpTimer(TimeSpan alertTime)
{
DateTime current = DateTime.Now;
TimeSpan timeToGo = alertTime - current.TimeOfDay;
if (timeToGo < TimeSpan.Zero)
{
return;//time already passed
}
this.timer = new System.Threading.Timer(x =>
{
this.SomeMethodRunsAt1600();
}, null, timeToGo, Timeout.InfiniteTimeSpan);
}
private void SomeMethodRunsAt1600()
{
//this runs at 16:00:00
}
// usage
var timed = SetUpTimer(new TimeSpan(23, 45, 00));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment