1

I'm trying to use timers in a project. But I really feel I am lacking a good amount of knowledge about timers. I'm making a project where I need minimum 4 different actions based on time passed. I used the nice answer https://stackoverflow.com/a/13019471/13903153 That made med very happy, since I can easily use it, while not understand it 100%

BUT - when I make 2 timers they both trigger when the first timer triggers, the first time. Then the timers are back to the normal schedule.

public static void StartMSGT()
    {

        var startTimeSpan = TimeSpan.Zero;
        var startTimeSpan2 = TimeSpan.Zero;
        var periodTimeSpan = TimeSpan.FromSeconds(5);
        var periodTimeSpan2 = TimeSpan.FromMinutes(2);
        Console.WriteLine("timer have started!");
        System.Threading.Thread.Sleep(5000);
        var timer = new System.Threading.Timer((e) =>
        {
            Console.WriteLine("Timer hits");
        }, null, startTimeSpan, periodTimeSpan);

        var timer2 = new System.Threading.Timer((e) =>
        {
            Console.WriteLine("Timer2 hits");
        }, null, startTimeSpan2, periodTimeSpan2);
    }

This is my result:

Starting in 10 sek
Starting in 5 sek
timer have started!
Waiting for match to start
Timer2 hits
Timer hits
Timer hits
Timer hits
Timer hits
Timer hits

In the second timer, I had "startTimespan" just like in the first timer, but that did not seem to make a difference.

If someone could tell me a simplified explanation how it works and maybe also a solution. I would appreciate it very much.

Goodies
  • 1,951
  • 21
  • 26
  • I think you mean "trigger", not "tricker"... – Rufus L Jul 17 '20 at 23:40
  • It's not clear to me what behavior you're expecting to see that's not happening. Both timers have a start time of `0` (so they start right away). The first one has an interval of `5` seconds and the second one has an interval of `2` minutes, so those are the frequencies when they will run again. That's the behavior you're seeing, and it's documented [here](https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer.-ctor?view=netcore-3.1#System_Threading_Timer__ctor_System_Threading_TimerCallback_System_Object_System_Int32_System_Int32_). What did you expect to happen? – Rufus L Jul 17 '20 at 23:50
  • [REVIEW] hi Christian, welcome.. I solved the spelling issues, removed "noob question" from your title. See below, there is an answer ! – Goodies Jul 18 '20 at 00:13
  • NOW I understand Thx man – Christian Herrig Jul 18 '20 at 00:15

1 Answers1

1

Use the same period to start the timer:

var timer = new System.Threading.Timer((e) =>
{
    Console.WriteLine("Timer hits");
}, null, periodTimeSpan, periodTimeSpan);

var timer2 = new System.Threading.Timer((e) =>
{
    Console.WriteLine("Timer2 hits");
}, null, periodTimeSpan2, periodTimeSpan2);

The 3rd parameter in this Timer constructor is:

dueTime The amount of time to delay before callback is invoked, in milliseconds. Specify Infinite to prevent the timer from starting. Specify zero (0) to start the timer immediately.

So if you specify 0 timer will start immediately and invoke the first callback.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • Thank you. I was not aware the the first startTimeSpan2 was the timer for first trigger, I thought it was to start the timer... Does it then mean that the "var timer = new System.Threading.Timer" is the starting point of the timer? – Christian Herrig Jul 18 '20 at 00:16