How to make it so that when you start the timer, it immediately executes the code in the attached method, and not wait for the interval to start.

For example:

var makeWorkTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(15) }; makeWorkTimer.Tick += (sender, args) => { Console.WriteLine("Hello"); }; makeWorkTimer.Start(); 

How to make cw execute immediately, without waiting for the 15 second interval on the first run of the method?

    1 answer 1

     var makeWorkTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(15) }; EventHandler handler = (sender, args) => { Console.WriteLine("Hello"); }; makeWorkTimer.Tick += handler; makeWorkTimer.Start(); handler(null, null); 
    • Great, but better to call like this: handler(this, EventArgs.Empty) - Nikita
    • one
      Everything works, only the type of handler needs to be replaced by System.EventHandler instead of Action - Ep1demic
    • @ Ep1demic, corrected. - Qwertiy