我基本上希望调度程序计时器对象只执行一次。
所以我有基本代码:
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 4);
dispatcherTimer.Start();
然后在点击事件中:
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
// Now stop timer execution.. or kill the timer object
}
如何在执行后停止计时器或终止对象?
答案 0 :(得分:36)
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
(sender as DispatcherTimer).Stop();
}
答案 1 :(得分:22)
以下是使用lambda表达式的替代代码:
var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(4)};
timer.Tick += (sender, args) =>
{
this.Visibility = System.Windows.Visibility.Visible;
timer.Stop();
};
timer.Start();
答案 2 :(得分:6)
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
this.Visibility = System.Windows.Visibility.Visible;
var dispatcherTimer = (DispatcherTimer)sender;
dispatcherTimer.Stop();
}
答案 3 :(得分:2)
答案可能会解决,但是当你不再需要计时器时,你应该从Tick事件中分离你的事件监听器。我不相信eventhandler和垃圾收集器;)
我对DispatcherTimer的这个很好的子类做了一个很好的单一滴答计时器的类。
public class DispatcherTimeout : DispatcherTimer
{
#region Constructors and Destructors
protected DispatcherTimeout(DispatcherPriority priority)
: base(priority)
{
}
#endregion
#region Public Properties
public Action<DispatcherTimeout> Callback { get; set; }
#endregion
#region Public Methods and Operators
/// <summary>
/// Instantiates a new DispatcherTimeout and starts it.
/// </summary>
/// <param name="priority">
/// The dispatcher priority used for the timer.
/// </param>
/// <param name="duration">
/// The duration.
/// </param>
/// <param name="callback">
/// The callback which should be called on tick.
/// </param>
/// <returns>
/// An instance of DispatcherTimeout.
/// </returns>
public static DispatcherTimeout Timeout(DispatcherPriority priority, TimeSpan duration, Action<DispatcherTimeout> callback)
{
var dispatcherTimeout = new DispatcherTimeout(priority);
dispatcherTimeout.Interval = duration;
dispatcherTimeout.Callback = callback;
dispatcherTimeout.Tick += dispatcherTimeout.HandleTick;
dispatcherTimeout.Start();
return dispatcherTimeout;
}
#endregion
#region Methods
private void HandleTick(object sender, EventArgs e)
{
this.Stop();
this.Tick -= this.HandleTick;
if (this.Callback != null)
{
this.Callback(this);
}
}
#endregion
}
示例:
DispatcherTimeout.Timeout(
DispatcherPriority.Normal,
TimeSpan.FromSeconds(2.0),
timeout =>
{
this.Visibility = System.Windows.Visibility.Visible;
});
答案 4 :(得分:0)
/// <summary>
/// Fires an action once after "seconds"
/// </summary>
public static void FireOnce(float seconds, Action onElapsed)
{
Action detach = null;
var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(seconds) };
var handler = new EventHandler((s, args) =>
{
onElapsed();
// Note: When stop is called this DispatcherTimer handler will be GC'd (eventually). There is no need to unregister the event.
timer.Stop();
if (detach != null)
detach();
});
detach = new Action(() => timer.Tick -= handler); // No need for deregistering but just for safety let's do it.
timer.Tick += handler;
timer.Start();
}