6
A
回答
3
我得到了现在,我敢肯定,这不是防弹所以告诉我什么是错的吧...
Public Class ImprovedTimer
Inherits System.Timers.Timer
Private _sw As System.Diagnostics.Stopwatch
Private _paused As Boolean
Private _originalInterval As Double
Private _intervalRemaining As Double?
Public ReadOnly Property IntervalRemaining() As Double?
Get
Return _intervalRemaining
End Get
End Property
Public ReadOnly Property Paused() As Boolean
Get
Return _paused
End Get
End Property
Public ReadOnly Property OriginalInterval() As Double
Get
Return _originalInterval
End Get
End Property
Public Sub Pause()
If Me.Enabled Then
_intervalRemaining = Me.Interval - _sw.ElapsedMilliseconds
_paused = True
resetStopWatch(False, False)
MyBase.Stop()
End If
End Sub
Public Sub [Resume]()
If _paused Then
Me.Interval = If(_intervalRemaining.HasValue, _intervalRemaining.Value, _originalInterval)
resetStopWatch(True, False)
MyBase.Start()
End If
End Sub
Public Overloads Property Enabled() As Boolean
Get
Return MyBase.Enabled
End Get
Set(ByVal value As Boolean)
MyBase.Enabled = value
resetStopWatch(MyBase.Enabled, True)
End Set
End Property
Public Overloads Sub Start()
resetStopWatch(True, True)
MyBase.Start()
End Sub
Public Overloads Sub [Stop]()
resetStopWatch(False, True)
MyBase.Stop()
End Sub
Public Overloads Property Interval() As Double
Get
Return MyBase.Interval
End Get
Set(ByVal value As Double)
MyBase.Interval = value
If Not _paused Then
_originalInterval = MyBase.Interval
End If
End Set
End Property
Private Sub resetStopWatch(ByVal startNew As Boolean, ByVal resetPause As Boolean)
If _sw IsNot Nothing Then
_sw.Stop()
_sw = Nothing
End If
If resetPause Then
If _paused Then
Me.Interval = _originalInterval
End If
_paused = False
_intervalRemaining = Nothing
End If
If startNew Then
_sw = System.Diagnostics.Stopwatch.StartNew
End If
End Sub
Private Sub ImprovedTimer_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
resetStopWatch(False, True)
End Sub
Private Sub ImprovedTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Me.Elapsed
resetStopWatch(Me.AutoReset, True)
End Sub
End Class
6
没有停顿(),你可以写一个关于暂停():
- 变化(Timeout.Infinite,Timeout.Infinite)
- 保存计算计时器的剩余量。
- 变化(剩余计时器量)
如果你写了这个类,请张贴在回答,因为它似乎我们很多人需要这些功能:
脱离Timer类。 :)
+0
我刚刚做到了,你觉得怎么样?在1小时后回来 – Fredou 2009-04-14 19:47:32
0
你应该按照Shay Erlichmen给出的建议。您需要在暂停时保存剩余的时间,并在计时器恢复时从该点继续。至于什么是错了你当前的代码:
Me.Interval = Me.Interval - sw.ElapsedMilliseconds
上面的代码将确保你继续下一次它会工作打算的第一跳,但在continouos蜱你必须Me.Interval - sw.ElapsedMilliseconds作为间隔而不是原始设置间隔。
-1
创建这样一个计时器:
System.Timers.Timer t = new System.Timers.Timer(1000)
t.Elapsed += new System.Timers.ElapsedEventHandler(timerEvent);
public void timerEvent(object source, System.Timers.ElapsedEventArgs e)
{
}
可以将此属性设置为启动或暂停计时器执行timeEvent:
开始:
t.Enabled = true
暂停:
t.Enabled = false
0
这里是我一直使用的是什么。这可能不是最先进的准确,但工作很好。至少,对于试图在计时器中暂停/恢复的人来说,这是一个很好的起点。
public class PausableTimer
{
private Timer _timer;
private Stopwatch _stopWatch;
private bool _paused;
private double _interval;
private double _remainingTimeBeforePause;
public PausableTimer(double interval, ElapsedEventHandler handler)
{
_interval = interval;
_stopWatch = new Stopwatch();
_timer = new Timer(interval);
_timer.Elapsed += (sender, arguments) => {
if (handler != null)
{
if(_timer.AutoReset)
{
_stopWatch.Restart();
}
handler(sender, arguments);
}
};
_timer.AutoReset = false;
}
public bool AutoReset
{
get
{
return _timer.AutoReset;
}
set
{
_timer.AutoReset = value;
}
}
public void Start()
{
_timer.Start();
_stopWatch.Restart();
}
public void Stop()
{
_timer.Stop();
_stopWatch.Stop();
}
public void Pause()
{
if(!_paused && _timer.Enabled)
{
_stopWatch.Stop();
_timer.Stop();
_remainingTimeBeforePause = Math.Max(0, _interval - _stopWatch.ElapsedMilliseconds);
_paused = true;
}
}
public void Resume()
{
if(_paused)
{
_paused = false;
if(_remainingTimeBeforePause > 0)
{
_timer.Interval = _remainingTimeBeforePause;
_timer.Start();
}
}
}
}
相关问题
- 1. 用activemq“骆驼”暂停路线的正确方法是什么?
- 2. 什么是从UI线程暂停AsyncTask的正确方法
- 3. 当不在视图中时暂停动画的正确方法
- 4. 处理System.Timers.Timer的正确方法是什么?
- 5. SlidesJS暂停解决方法?
- 6. 可可 - 暂停方法
- 7. 如何正确暂停/冻结CameraCaptureSession
- 8. Phonegap暂停事件行为不正确
- 9. 如何停止System.Timers.Timer
- 10. 停止一个System.Timers.Timer
- 11. 停止IntentService的正确方法
- 12. 用MPI停止的正确方法
- 13. 正确的方法来停止侦听UDP套接字与RxExtensions
- 14. 哪个是在delphi 2007中启动暂停线程的正确方法?
- 15. 无法获得暂停按钮来恢复时间正确 - Excel VBA
- 16. 用正确的方法来努力来命名这个方法
- 17. 正确的方法来做片段transacrtion
- 18. 正确的方法来评估代码
- 19. 正确的方法来划分CAShapeLayer
- 20. 正确的方法来做swift投票?
- 21. 正确的方法来拉伸元素?
- 22. 正确的方法来清理密码?
- 23. 正确的方法来解除MFMailComposeViewController
- 24. 正确的方法来覆盖Control.ControlCollection
- 25. 正确的方法来关闭分段
- 26. 正确的方法来使用sessionFactory
- 27. 几次使用System.Timers.Timer的调用方法?
- 28. 在调用方法后暂停线程
- 29. 使用睡眠暂停方法一秒
- 30. 开始/暂停/恢复/暂停...由其他类调用的方法
我要回家了,所以回到大约1小时后,看看我收到了什么回复。 – Fredou 2009-04-14 19:46:20
sw应该是_sw。 – 2009-04-14 20:43:19
我不知道我是否会在Pause()中更新Interval,我可能会保留私有值。 – 2009-04-14 20:44:34