本文介绍了如何停止/恢复剩余时间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。



任何人都可以帮助我。





谢谢

解决方案





相当简单,在代码中添加一个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

解决方案




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#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 00:19