如何停止/恢复剩余时间C#? [英] How to stop/resume remaining time C#?

查看:116
本文介绍了如何停止/恢复剩余时间C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我有一个Windows应用程序,我必须在其中显示'剩余时间'。



现在,我要求停止/恢复剩余时间。



我尝试了什么:



//剩余时间代码

公共TimeSpan RemainingTime 
{
get
{
_elapsedTime = DateTime.Now - StartTime;
TimeSpan remainingTime = Duration - _elapsedTime;

if(remainingTime> TimeSpan.Zero)
return remainingTime;
其他
返回TimeSpan.Zero;
}
}





这里的问题是,我正在使用DateTime.Now来计算余下的所以,我无法停止/恢复剩余的时间。即使我正在使用timerTick_Event。



任何人都可以帮助我。





谢谢

解决方案

如果你使用一个开始时间和现在来计算到目前为止的运行时间(这是正确的方法,Tick事件不是实时的,所以他们漂移 如果你认为它们是准确的那么暂停计时器的唯一方法是存储你点击暂停时经过的时间,并根据当时的当前时间重新计算取消暂停时的开始时间。

暂停:
elapsedTime = DateTime.Now - StartTime;
paused = true;



 UnPause:
StartTime = DateTime.Now - elapsedTime;
暂停= false ;


而不是使用 DateTime 。现在要测量已用时间,请使用秒表类 [ ^ ]。

 私人秒表_timer; 

public void Start()
{
_timer = Stopwatch.StartNew();
}

public void 暂停()
{
if (_timer null throw new InvalidOperationException();
if (_timer.IsRunning)_timer.Stop();
}

public void Resume()
{
if (_timer null throw new InvalidOperationException();
if (!_timer.IsRunning)_timer.Start();
}

public TimeSpan RemainingTime
{
get
{
if (_timer null return TimeSpan.MaxValue;

TimeSpan remainingTime = Duration - _timer.Elapsed;
返回 remainingTime > TimeSpan.Zero? remainingTime:TimeSpan.Zero;
}
}


Quote:

现在,我需要停止/恢复剩余时间。



相当简单,在代码中添加一个OffsetTime变量。

表示已用时间:

开始时,OffsetTime = 0
运行时
,ElapsedTime = DateTime.Now - StartTime + OffsetTime
停止时
,OffsetTime = DateTime。现在 - StartTime + OffsetTime

停止时,ElapsedTime = OffsetTime

for CountDown:

启动时,只需将OffsetTime设置为剩余时间


Dear All,

I have a windows application in which I have to display the 'Remaining Time'.

Now, I have a requirement to stop/resume the 'Remaining Time'.

What I have tried:

//REMAIN TIME CODE

public TimeSpan RemainingTime
        {
            get
            {
                _elapsedTime = DateTime.Now - StartTime;
                TimeSpan remainingTime = Duration - _elapsedTime;

                if (remainingTime > TimeSpan.Zero)
                    return remainingTime;
                else
                    return TimeSpan.Zero;
            }
        }



Here, the problem is, I'm using DateTime.Now to calculate the remaining, so, I'm not able to stop/resume the remaining time. Even though I'm using timerTick_Event.

Can anyone please help me.


Thanks

解决方案

If you use a start time and Now to calculate the "run time so far" (which is the right way, Tick events aren't real time, so they "drift" if you assume they are accurate) then the only way to "Pause the timer" is to store the elapsed time when you hit "pause" and recalculate the "start time" when you unpause, based on the current time at that point.

Pause:
   elapsedTime = DateTime.Now - StartTime;
   paused = true;


UnPause:
   StartTime = DateTime.Now - elapsedTime;
   paused = false;


Rather than using DateTime.Now to measure the elapsed time, use the Stopwatch class[^].

private Stopwatch _timer;

public void Start()
{
    _timer = Stopwatch.StartNew();
}

public void Pause()
{
    if (_timer is null) throw new InvalidOperationException();
    if (_timer.IsRunning) _timer.Stop();
}

public void Resume()
{
    if (_timer is null) throw new InvalidOperationException();
    if (!_timer.IsRunning) _timer.Start();
}

public TimeSpan RemainingTime
{
    get
    {
        if (_timer is null) return TimeSpan.MaxValue;
        
        TimeSpan remainingTime = Duration - _timer.Elapsed;
        return remainingTime > TimeSpan.Zero ? remainingTime : TimeSpan.Zero;
    }
}


Quote:

Now, I have a requirement to stop/resume the 'Remaining Time'.


Rather simple, add an OffsetTime variable to your code.
for elapsed time:
on start, OffsetTime = 0
on run, ElapsedTime = DateTime.Now - StartTime + OffsetTime
on stop, OffsetTime = DateTime.Now - StartTime + OffsetTime
when stopped, ElapsedTime = OffsetTime
for CountDown:
on start, just set OffsetTime to remaining time


这篇关于如何停止/恢复剩余时间C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆