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.