question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked JackJJun-MSFT commented

C# Lambda, callback notation, best practices

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.

dotnet-csharpwindows-forms
spelling-12.png (69.9 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MarkusFreitag-0088, is any update? Please check if my answer works for you.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
1 Vote"
JackJJun-MSFT answered

@MarkusFreitag-0088, 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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.