0

I would like to execute method at a specified time e.g. execute some method in five minutes. I don't need to execute it in interval ( every day or every hour), just once. I making web application with asp.net core 2.1. I would like to be able to add e.g. 3 jobs to do 1) in 5 min 2) in 10 min 3) in 15 min and expecting to perform when time is over. How can I do it in a simple way?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Indigo_heart
  • 129
  • 11

1 Answers1

1

Why not use timers. Simple and easy...

private System.Timers.Timer myTimer;
private int minutes = Settings.Default.5MinuteTimer;

private void SetTimer()
{
    myTimer= new System.Timers.Timer(1000 * 60 * minutes);          
    myTimer.Elapsed += OnTimedEvent;
    myTimer.AutoReset = false; //Fire event only once.
    myTimer.Enabled = true;
}

private void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    MyMethod();
}

private MyMethod()
{
    myTimer.Stop(); //Calling stop will also Dispose the timer.
    // Other code.
}
Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • ok, but if i would like to know how much time is left? is there easy way to check this. e.g. I planned to do some work in 5 hours. After 3 hours I want to know how much longer do I have to wait. I think that I have to do it for my own but maybe you can help me – Indigo_heart Nov 12 '18 at 19:54
  • 1
    @Indigo_heart Check out this answer... https://stackoverflow.com/a/2278550/229930 – Dean Kuga Nov 12 '18 at 20:05