从 N 秒、分钟、小时开始 DispatcherTimer

Start DispatcherTimer from N seconds, minutes, hours

我一直在搜索信息,但找不到任何信息。我的问题是如何从 N 秒或分钟或小时开始 DispatcherTimer。我的意思是目前它从 00:00:00 开始(也显示在 CurrentTime 标签中)但是如果我想从 00:00:30 开始(也显示在 CurrentTime 标签中),应该有什么不同?

进一步说明...当我启动一个应用程序时,我执行 StartWorkingTimeTodayTimer()。然后我有一个开始显示应用程序运行时间的标签 (CurrentTime)。目前它从 00:00:00 开始。我想在开始时显示例如 00:00:30,然后像现在一样按一秒打勾……所以 00:00:30 -> 00:00:31 -> 00:00:32 -> 00:00:33 -> 00:00:34 ->

我试过玩:

DateTime x30SecsLater = StartTimeWholeDay.AddSeconds(30);

没有成功。

当前代码:

private static DateTime StartTimeWholeDay;
private DispatcherTimer _dailyTimer;

public void StartWorkingTimeTodayTimer()
{
    StartTimeWholeDay = DateTime.Now;
    DateTime x30SecsLater = StartTimeWholeDay.AddSeconds(30);

    _dailyTimer = new DispatcherTimer(DispatcherPriority.Render);
    _dailyTimer.Interval = TimeSpan.FromSeconds(1);
    _dailyTimer.Tick += (sender, args) =>
    {
        CurrentTime = (DateTime.Now - StartTimeWholeDay).ToString(@"hh\:mm\:ss"); // DateTime.Now.ToLongTimeString()
    };
    _dailyTimer.Start();
} 

编辑:

我已经试过了:

public void StartWorkingTimeTodayTimer()
{
    StartTimeWholeDay = DateTime.Now;
    DateTime x30SecsLater = StartTimeWholeDay.AddSeconds(30);

    _dailyTimer = new DispatcherTimer(DispatcherPriority.Render);
    _dailyTimer.Interval = TimeSpan.FromSeconds(1);
    _dailyTimer.Tick += (sender, args) =>
    {
        CurrentTime = (DateTime.Now - x30SecsLater).ToString(@"hh\:mm\:ss"); // DateTime.Now.ToLongTimeString()
    };
    _dailyTimer.Start();
}

但它向后计算...

应该反过来 00:00:30 -> 00:00:31 -> 00:00:32 -> 00:00:33 -> 00:00:34 ->

为了获得 WPF 运行 控制台(在 .NET 5 中)花了很长时间......我失败了,所以我将使用 System.Timers 给出答案。但是你明白了。

using System;
using System.Timers;

namespace TimerExample
{
    class Program
    {
        private static DateTime _startTime;

        private static void OnTimedEvent(object source, ElapsedEventArgs eea)
        {
            Console.WriteLine((eea.SignalTime - _startTime)
                .Add(TimeSpan.FromSeconds(29)) // 29, because the first trigger happens 1 sec after start
                .ToString(@"hh\:mm\:ss"));
        }

        static void Main(string[] args)
        {
            _startTime = DateTime.Now;
            var timer = new Timer
            {
                Interval = 1000, // msec
                AutoReset = true,
            };
            timer.Elapsed += OnTimedEvent;
            timer.Start();

            Console.WriteLine("Press any key to stop");
            Console.ReadKey();
        }
    }
}

using System;
using System.Timers;

namespace TimerExample
{
    class Program
    {
        private static DateTime _startTime;

        private static void OnTimedEvent(object source, ElapsedEventArgs eea)
        {
            Console.WriteLine((eea.SignalTime - _startTime)
                .ToString(@"hh\:mm\:ss"));
        }

        static void Main(string[] args)
        {
            _startTime = DateTime.Now.AddSeconds(-29); // again 29 instead of 30
            var timer = new Timer
            {
                Interval = 1000, // msec
                AutoReset = true,
            };
            timer.Elapsed += OnTimedEvent;
            timer.Start();

            Console.WriteLine("Press any key to stop");
            Console.ReadKey();
        }
    }
}