简体   繁体   中英

Monitoring timers in a windows service

I have the following code in my windows service

  var timeToWait = TimeSpan.FromSeconds(20);
        var interval = TimeSpan.FromMinutes(5);
        var t = new Timer((s) =>
            {
                tracker.ProcessAuditLogs();
            }, null, timeToWait, interval);

Are there any ways I can monitor this timer, by polling or any other mechanisms, and get back information to display on a windows form, with information such as

  • Last executed time (eg, process executed 7 mins ago)
  • Next execution (time of next execution, eg, if it's a 20 min interval, and 5 mins has passed since the last execution, it will show '15 mins to next execution'

There isn't a standard way with the Timer class. I would advise to encapsulate a timer object in a parent class like this :

public class MonitoredTimer {

    private Timer _timer;
    private int NextExecutionTime;
    private int LastExecutionTime

    public MonitoredTimer(TimerCallback callback,...) {
        _timer = new Timer((s)=>callback(s);UpdateExecutionTimes());
    }

    public int GetLastExecution(){
        return LastExecutionTime;
    }

    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM