C# Lambda, callback notation, best practices

Markus Freitag 3,791 Reputation points
2022-04-26T12:52:36.207+00:00

Hello,
I have partly problems about them correct notation with lambda.

public System.Threading.Timer AsyncProgramChangeTimer = null;  
        public System.Threading.Timer AsyncProgramChangeTimer2 = null;  
        public void MyStartTimer()  
        {  
            lock (this)  
            {  
                AsyncProgramChangeTimer = new System.Threading.Timer((o) =>  
                {  
                    if (ProgramChangeDone == false)  
                    {  
                        //do whatever  
                        Trace.WriteLine("Timer was coming!!!");  
                        Action<string> DelegateTeste_ModifyText = THREAD_MOD;  
                        Invoke(DelegateTeste_ModifyText, $"Timer was coming!!! {DateTime.Now}");  
                        AsyncProgramChangeTimer.Change(2000, Timeout.Infinite);  
                    }  
                    else  
                    {  
                        Trace.WriteLine("Timer no repeat!!!");  
                    }  
                }, null, 0, Timeout.Infinite);  
            }  
  
            AsyncProgramChangeTimer2 = new System.Threading.Timer(CallBackTimer, null, 0, Timeout.Infinite);  
        }  
  
        private void CallBackTimer(object state)  
        {  
            if (ProgramChangeDone == false)  
            {  
                //do whatever  
                Trace.WriteLine("Timer was coming!!!");  
                Action<string> DelegateTeste_ModifyText = THREAD_MOD;  
                Invoke(DelegateTeste_ModifyText, $"Timer was coming!!! {DateTime.Now}");  
                AsyncProgramChangeTimer.Change(2000, Timeout.Infinite);  
            }  
            else  
            {  
                Trace.WriteLine("Timer no repeat!!!");  
            }  
        }  

196537-spelling-12.png

Is there a trick, a recipe how to get on it. How do you do it? Simply write.
I hope you experts understand what I mean and could help me that I do easier in the future.

Many thanks in advance for the help.

Developer technologies Windows Forms
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-04-27T06:49:20.06+00:00

    @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.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.