It is necessary that the functions be performed in a linear sequence. But at different intervals.

Now i do so

delay((myDelegate)fun1, 2); delay((myDelegate)fun2, 5); delay((myDelegate)fun3, 8); delay((myDelegate)fun4, 10); delay((myDelegate)fun5, 15); delay((myDelegate)fun6, 20); private void delay(myDelegate fun, int time) { var _delayTimer = new System.Timers.Timer(); int minute_rnd = time; _delayTimer.Interval = minute_rnd * 1000; _delayTimer.AutoReset = false; _delayTimer.Elapsed += (s, args) => fun(); _delayTimer.Start(); } 

But this is a very clumsy implementation, as it sometimes sticks, and it works two consecutively and the sequence gets lost.

How can this be stably implemented?

  • What version of the .NET Framework do you use? - kmv
  • .NET Framework 4.5 - hitcode

2 answers 2

The answer @nzeemin is correct, but if you also need asynchrony, do this:

 async Task ExecuteAll() { await Task.Delay(2000); fun1(); await Task.Delay(3000); fun2(); await Task.Delay(3000); fun3(); await Task.Delay(2000); fun4(); await Task.Delay(5000); fun5(); await Task.Delay(5000); fun6(); } 

Note that fun1 and the rest must be fast enough, otherwise they should also be made asynchronous.

    If the sequence is linear, then why is there a timer at all?

     Thread.Sleep(2000); fun1(); Thread.Sleep(5000); fun2(); 

    ...

    • Sleep - slows down the response of the program. It seems to hang. Will not work. - hitcode