System.Threading Multiple Timers in C#

  • Thread starter Prof. 27
  • Start date
  • Tags
    Multiple
In summary: They pass the Timer object as the state parameter, and then use that to get at the counter property.In summary, the conversation is about creating two timers using System.threading.timer that run in parallel. The person is having trouble with getting nonsensical output when running the program, despite reading a lot of stack overflow questions. They share their code for creating the timers and using a timer callback function, but it differs from the example shown in the MSFT documentation for TimerCallback.
  • #1
Prof. 27
50
1

Homework Statement


Basically I'm trying to create two timers using System.threading.timer that run in parallel. One that runs for a minute, one that runs for a second. When I run the program I get nonsensical output. Despite reading a lot of stack overflow questions I can't seem to find where I've gone wrong.

Homework Equations


None

The Attempt at a Solution


Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace TimerTestFour
{
    class TimerState
    {
        public int counter = 0;
        public Timer tmr;
    }
    class Program
    {
        static void Main(string[] args)
        {
            TimerState s = new TimerState();
            TimerState t = new TimerState();

            TimerCallback timerDelegate = new TimerCallback(CheckStatus);
            TimerCallback timerDelegateTwo = new TimerCallback(CheckStatus);

            Timer timerS = new Timer(timerDelegate, s, 0, 60000);
            Timer timerT = new Timer(timerDelegateTwo, t, 0, 1000);

            s.tmr = timerS;
            t.tmr = timerT;

            while ((s.tmr != null) && (t.tmr != null))
            {
                Thread.Sleep(0);
                Console.WriteLine("Timers done.");
            }
        }
        static void CheckStatus(Object state)
        {
            TimerState z = (TimerState)state;
            z.counter++;
            Console.WriteLine("{0} Checking status {1}.", DateTime.Now.TimeOfDay, z.counter);
            if (z.counter == 1)
            {
                Console.WriteLine("Dispose Timer");
                z.tmr.Dispose();
                z.tmr = null;
            }
        }
    }
}
 
Physics news on Phys.org

Related to System.Threading Multiple Timers in C#

1. How do I create multiple timers in C# using System.Threading?

To create multiple timers in C# using System.Threading, you can use the Timer class. This class allows you to specify a callback method to be executed when the timer elapses. You can create multiple instances of the Timer class to create multiple timers, each with their own unique callback method.

2. Can I run multiple timers simultaneously in C#?

Yes, you can run multiple timers simultaneously in C# by creating multiple instances of the Timer class and starting them at the same time. However, it is important to note that the timers will execute on separate threads, so you need to ensure that your code is thread-safe.

3. How do I stop a specific timer in C# using System.Threading?

To stop a specific timer in C# using System.Threading, you can use the Timer class's Stop method. This will stop the timer from executing any further and will dispose of the timer's resources. Alternatively, you can use the Dispose method to manually stop and dispose of the timer.

4. Is it possible to change the interval of a running timer in C# using System.Threading?

Yes, it is possible to change the interval of a running timer in C# using System.Threading. You can use the Change method of the Timer class to change the timer's interval while it is running. This can be useful if you need to dynamically adjust the timer's frequency based on certain conditions.

5. How can I handle exceptions that occur within the callback method of a timer in C# using System.Threading?

To handle exceptions that occur within the callback method of a timer in C# using System.Threading, you can use a try/catch block within the callback method. This will allow you to catch any exceptions and handle them appropriately. Additionally, you can also use the UnhandledException event of the Timer class to handle any uncaught exceptions that may occur during the timer's execution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
910
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
999
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Precalculus Mathematics Homework Help
Replies
8
Views
1K
Back
Top