开发者

DispatcherTimer tick once

开发者 https://www.devze.com 2023-01-22 14:15 出处:网络
I basically want a dispatcher timer object to only execute once. So I have the basic code: DispatcherTimer dispatcherTimer = new DispatcherTimer();

I basically want a dispatcher timer object to only execute once.

So I have the basic code:

DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 4);
dispatcherTimer.Start();

Then inside the click event:

private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            // Now stop timer execution.. or kill the timer object
        }

How can I stop the timer or kill off the object after this exe开发者_StackOverflowcution?


private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            (sender as DispatcherTimer).Stop();
        }


Here is alternative code using lambda expression:

var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(4)};
timer.Tick += (sender, args) => 
{
    this.Visibility = System.Windows.Visibility.Visible;
    timer.Stop();
};

timer.Start();


private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            this.Visibility = System.Windows.Visibility.Visible;
            var dispatcherTimer = (DispatcherTimer)sender;
            dispatcherTimer.Stop();
        }     


the answers might work out but you should detach your event listener from the Tick Event when you do not need the timer any longer. I don't trust eventhandler and the garbage collector ;)

I worte this nice subclass of DispatcherTimer to have a convinient class for single tick timers.

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
}

Example:

DispatcherTimeout.Timeout(
    DispatcherPriority.Normal,
    TimeSpan.FromSeconds(2.0),
    timeout => 
    {
        this.Visibility = System.Windows.Visibility.Visible;
    });


    /// <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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号