如何在 WP7 中暂停计时器?在我的应用程序中,我想暂停和恢复计时器。但是 DispatcherTimer 只有启动和停止。如何暂停计时器?
问问题
1932 次
6 回答
1
试试这个:定义一个全局变量:
private static DateTime EndTime { get; set; }
然后,当按下 Start 按钮时,您进行以下检查以确定计时器是否已停止或暂停:
private void btnStartClick(object sender, RoutedEventArgs e)
{
if (this.dispatcherTimer == null)
{
this.dispatcherTimer = new DispatcherTimer();
this.dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
this.dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
}
if (this.EndTime == DateTime.MinValue)
{
this.EndTime = DateTime.Now + (TimeSpan)this.timeSpan.Value;
}
this.dispatcherTimer.Start();
}
接下来,当按下暂停按钮时,您可以停止计时器,因为下次按下开始按钮时,您将通过上述检查:
private void btnPauseClick(object sender, RoutedEventArgs e)
{
this.dispatcherTimer.Stop();
}
于 2012-07-11T20:04:04.027 回答
0
对于暂停/恢复功能,您必须将其构建到您的 Tick 处理程序中,启动和停止计时器。
如果您的计时器间隔约为 60 秒,请尝试以下操作:
dispatcher_Timer.Interval = TimeSpan.Fromseconds(30);
dispatcher_Timer.Tick += new EventHandler(dispatcherTimer_Tickon);
dispatcher_Timer.Start();
private void dispatcherTimer_Tickon(object sender, EventArgs e)
{
dispatcher_Timer.Tick -= new EventHandler(dispatcherTimer_Tickon);
// Do the work for 30 seconds and pause it using stop
dispatcher_Timer.Stop();
dispatcher_Timer.Tick += new EventHandler(dispatcherTimer2_Tickon);
dispatcher_Timer.Start();
}
private void dispatcherTimer2_Tickon(object sender, EventArgs e)
{
dispatcher_Timer.Tick -= new EventHandler(dispatcherTimer2_Tickon);
// Do the remaining work for 30 seconds and pause it using stop
dispatcher_Timer.Stop();
}
于 2012-07-11T07:44:00.627 回答
0
尝试将 IsEnabled 属性设置为 false 以停止它,然后在您希望它继续时返回 true。
于 2012-07-11T07:37:54.923 回答
0
没有办法暂停计时器,然后让它从中断的地方继续。IsEnabled 只调用 Start 和 Stop。
您需要创建自己的计时器才能拥有此功能。
于 2012-07-11T07:47:08.417 回答
0
试试这个代码:
public class DispatcherTimerEx : DispatcherTimer
{
public new TimeSpan Interval
{
get => maxInterval;
set => base.Interval = maxInterval = value;
}
public new bool IsEnabled
{
get => base.IsEnabled;
set
{
if (value == base.IsEnabled)
return;
if (value)
this.Start();
else
this.Stop();
}
}
TimeSpan maxInterval;
DateTime startTime = DateTime.MinValue;
DateTime stopTime = DateTime.MinValue;
public DispatcherTimerEx()
{
base.Tick += OnTick;
}
public DispatcherTimerEx(DispatcherPriority priority) : base(priority)
{
base.Tick += OnTick;
}
public DispatcherTimerEx(DispatcherPriority priority, Dispatcher dispatcher) : base(priority, dispatcher)
{
base.Tick += OnTick;
}
public DispatcherTimerEx(TimeSpan interval, DispatcherPriority priority, EventHandler callback,
Dispatcher dispatcher) : base(interval, priority, callback, dispatcher)
{
base.Tick += OnTick;
}
public new void Start()
{
base.Start();
startTime = DateTime.Now;
stopTime = DateTime.MinValue;
}
void OnTick(object sender, EventArgs e)
{
startTime = DateTime.Now;
if (base.Interval == maxInterval) return;
base.Stop();
base.Interval = maxInterval;
base.Start();
}
public void Pause()
{
base.Stop();
stopTime = DateTime.Now;
}
public void Resume()
{
if (startTime == DateTime.MinValue)
startTime = DateTime.Now;
if (stopTime == DateTime.MinValue)
{
base.Interval = maxInterval;
}
else
{
base.Interval = maxInterval - (stopTime - startTime);
stopTime = DateTime.MinValue;
}
base.Start();
}
}
我在这里添加了Puase()
方法Resume()
。
于 2021-12-04T02:16:10.957 回答
0
你可以通过这个简单的技巧轻松地暂停 Dispatcher,如果你将时间间隔设置为零呢?
这将暂停调度程序
dispatcherTimer.Interval.Subtract(dispatcherTimer.Interval);
再次恢复 dispatchTimer 只需将时间跨度添加到 Interval。
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 5); // this will
或者
dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
于 2021-05-12T19:21:25.760 回答