System.Threading Multiple Timers in C#

  • Thread starter Thread starter Prof. 27
  • Start date Start date
  • Tags Tags
    Multiple
Click For Summary
SUMMARY

The discussion focuses on implementing multiple timers in C# using the System.Threading.Timer class. The user attempts to create two timers: one that triggers every minute and another every second, but encounters issues with nonsensical output. The provided code demonstrates the creation of TimerState objects and the use of TimerCallback delegates to manage timer events. A comparison with Microsoft's documentation reveals that the implementation of the CheckStatus method differs significantly from the recommended practices.

PREREQUISITES
  • Understanding of C# programming language
  • Familiarity with System.Threading namespace
  • Knowledge of Timer and TimerCallback classes
  • Basic concepts of multithreading in C#
NEXT STEPS
  • Review Microsoft documentation on TimerCallback for best practices
  • Explore the use of CancellationToken for managing timer lifecycles
  • Learn about the Task Parallel Library (TPL) for more efficient timer management
  • Investigate the differences between System.Threading.Timer and System.Timers.Timer
USEFUL FOR

C# developers, software engineers working with multithreading, and anyone implementing timer-based functionality in applications.

Prof. 27
Messages
49
Reaction score
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

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K