代码之家  ›  专栏  ›  技术社区  ›  JL. Hans Passant

调度员勾选一次

wpf
  •  20
  • JL. Hans Passant  · 技术社区  · 14 年前

    所以我有了基本代码:

    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
            }
    

    5 回复  |  直到 14 年前
        1
  •  39
  •   JL. Hans Passant    14 年前
    private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                this.Visibility = System.Windows.Visibility.Visible;
                (sender as DispatcherTimer).Stop();
            }
    
        2
  •  24
  •   Vlad Bezden    12 年前

    下面是使用lambda表达式的替代代码:

    var timer = new DispatcherTimer {Interval = TimeSpan.FromSeconds(4)};
    timer.Tick += (sender, args) => 
    {
        this.Visibility = System.Windows.Visibility.Visible;
        timer.Stop();
    };
    
    timer.Start();
    
        3
  •  6
  •   Mark    14 年前
    private void dispatcherTimer_Tick(object sender, EventArgs e)
            {
                this.Visibility = System.Windows.Visibility.Visible;
                var dispatcherTimer = (DispatcherTimer)sender;
                dispatcherTimer.Stop();
            }     
    
        4
  •  2
  •   toastedtoast    11 年前

    答案可能是正确的,但是当您不再需要计时器时,应该将事件侦听器与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;
        });
    
        5
  •  1
  •   RJ Thompson    7 年前
        /// <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();
        }