C# Timer problem - Periodic timer until end of signalling.

Markus Freitag 3,791 Reputation points
2022-02-03T09:12:16.607+00:00

Hello,
170886-timerproblem.png

I would like to start a timer immediately.
Every second a function is to check whether the flag 'Empty' is set. If yes, end timer.
If no, the function should be called again after one second.

How should the settings be set correctly?
How do I start the timer? How do I end the timer? Infinite I think is wrong.
Is my syntax wrong?

 public Timer AsyncProgramChangeTimer = null;  
        public void MyStartTimer()  
        {  
            lock (this)  
            {  
                //AsyncProgramChangeTimer = new Timer((o) =>  
                //{  
                //    lock (this)  
                //    {  
                //        AsyncPendingProgramId = null;  
                //    }  
                //}, null, 5000, Timeout.Infinite);  
  
                AsyncProgramChangeTimer = new Timer((o) =>  
                {  
                    lock (this)  
                    {  
                        AsyncPendingProgramId = null;  
                    }  
                }, null, 1000, 0);  
  
            }  
  
        }  
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

Accepted answer
  1. Yijing Sun-MSFT 7,096 Reputation points
    2022-02-04T02:27:41.22+00:00

    Hi @Markus Freitag ,
    A simple lock to synchronize may be enough to prevent multiple runs from occurring.
    That being said, it might be better to start a timer after you're operation is complete, and just use it one time, then stop it. Restart it after your next operation.

      AsyncProgramChangeTimer = new Timer((o) =>  
     {  
      //do whatever  
      timer.Change(1000, Timeout.Infinite);  
     }, null, 0, Timeout.Infinite);  
    

    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    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.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-02-03T09:51:16.993+00:00

    Perhaps alone these lines where you have a property to indicate if the timer should run. The class is intended to use as a base for forum questions and most likely will need tweaks for your specific needs.

    using System.Threading;
    using Timer = System.Threading.Timer;
    
    namespace WorkingWithTimer.Classes
    {
        public class Utilities
        {
            /// <summary>
            /// How long between intervals, adjust as needed or use <see cref="Initialize"/> overload
            /// </summary>
            private static int _dueTime = 6000 * 10;
            private static Timer _workTimer;
    
            public delegate void MessageHandler(string message);
            /// <summary>
            /// Optional event
            /// </summary>
            public static event MessageHandler Message;
            /// <summary>
            /// Flag to determine if timer should initialize 
            /// </summary>
            public static bool ShouldRun { get; set; } = true;
    
            private static void Initialize()
            {
                if (!ShouldRun) return;
                _workTimer = new Timer(Dispatcher);
                _workTimer.Change(_dueTime, Timeout.Infinite);
            }
    
            private static void Initialize(int dueTime)
            {
                if (!ShouldRun) return;
                _dueTime = dueTime;
                _workTimer = new Timer(Dispatcher);
                _workTimer.Change(_dueTime, Timeout.Infinite);
            }
            private static void Dispatcher(object e)
            {
                Worker();
                _workTimer.Dispose();
                Initialize();
            }
    
            public static void Start()
            {
                Initialize();
                Message?.Invoke("Started");
            }
            public static void Stop()
            {
                _workTimer.Dispose();
                Message?.Invoke("Stopped");
            }
    
            /// <summary>
            /// Where work is done
            /// </summary>
            private static void Worker()
            {
                Message?.Invoke("Performing work");
            }
    
        }
    }
    
    1 person found this answer helpful.

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.