@Markus Freitag , Welcome to Microsoft Q&A,
Based on my test, I think that you are right, the two methods are the same to create a timer.
AsyncProgramChangeTimer2 = new System.Threading.Timer(CallBackTimer, null, 0, Timeout.Infinite);
According to the Microsoft doc Timer said, CallBackTimer method need to have the same parameters with the TimerCallback Delegate.
Thefore, we need to set the object parameter like the following:
private void CallBackTimer(object state)
{
}
Then, I will tell you how the parameter pass in the CallBackTimer.
AsyncProgramChangeTimer2 = new System.Threading.Timer(CallBackTimer, null, 0, Timeout.Infinite);
The second parameter is object state parameter. According to the doc, it can be null:
An object containing application-specific information relevant to the method invoked by this delegate, or null. (Comes from TimerCallback Delegate)
We could use the following method to know how the parameter object passed in the timer.
int a = 1;
AsyncProgramChangeTimer2 = new System.Threading.Timer(CallBackTimer, a, 0, Timeout.Infinite);
private void CallBackTimer(object state)
{
if (ProgramChangeDone == false)
{
Console.WriteLine(state.ToString());
Console.WriteLine("Timer was coming!!!");
Action<string> DelegateTeste_ModifyText = THREAD_MOD;
Invoke(DelegateTeste_ModifyText, $"Timer was coming!!! {DateTime.Now}");
AsyncProgramChangeTimer2.Change(2000, Timeout.Infinite);
}
else
{
Console.WriteLine("Timer no repeat!!!");
}
}
After starting timer, we could also see it will output "1".
Also, the answer also has a good explanation for it, you could have a look.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.