Share via

Timer configuration

Noah Aas 1,210 Reputation points
2025-04-19T07:06:48.5566667+00:00
protected void StartFunction()
  {	 
	 ReqTimer = new System.Threading.Timer((o) =>
    {
	   // for one and two nort needed, right?
       // if (ReqTimer != null)
       //     ReqTimer.Change(1000 * 15, Timeout.Infinite);   

    }, null, 1000 * 15, System.Threading.Timeout.Infinite);
  }

enter image description here

Can someone please help me. I would like to map the three variants.

Timer only starts in x seconds Timer starts immediately Timer only starts in x seconds and makes an interval every n seconds.

How do I have to program this? Preferably with and without lambda (easier to read)

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

  1. Bruce (SqlWork.com) 84,061 Reputation points
    2025-04-19T23:42:36.31+00:00

    unlike the system.timer class which runs the callback on the same thread, the System.Threading runs the callback on a thread from the thread pool.

    using System;
    using System.Threading;
    
    var n = 3; // n seconds
    var counter = 0; // non thread safe counter
    var locker = new object(); // object to lock on
    
    // timer fire once now
    var timer1 = new Timer(
    	callback: new TimerCallback(TimerProc),
    	state: "timer1",        // state
    	dueTime: 0,             // start with no delay
    	period: Timeout.Infinite // only once
    );
    // timer fire only once after n seconds
    var timer2 = new Timer(
    	callback: new TimerCallback(TimerProc),
    	state: "timer2",        // state
    	dueTime: n * 1000,      // start in 1 second
    	period: Timeout.Infinite // only once
    );
    // timer fire only once after n seconds
    var timer3 = new Timer(
    	callback: new TimerCallback(TimerProc),
    	state: "timer3",        // state
    	dueTime: n * 1000,      // start in 1 seconds
    	period: 10 * 1000       // repeat every 10 seconds
    );
    
    // delay main thread until timer thread complete
    Thread.Sl eep(35000);
    timer3.Dispose(); // cancel running timer
    Console.WriteLine("all done");
    
    void TimerProc(object? state)
    {
        var t = 0;
        lock (locker)
        {
            // simulate non threadsafe  work
            var a = counter;
            Thread.Sl eep(1000);
            counter = a + 1;
            t = counter;
        }
    	Console.WriteLine($"{state}: counter: {t} thread: {Thread.CurrentThread.ManagedThreadId}");
    }
    
    

    note: because thread2 and thread3 start at the same time, its undefined which runs and completes first.

    Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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