WPF C# - 计时器倒计时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18230552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:26:05  来源:igfitidea点击:

WPF C# - Timer countdown

c#wpftimerdispatchertimer

提问by touyets

How can I implement the following in my piece of code written in WPF C#?

如何在用 WPF C# 编写的代码段中实现以下内容?

I have a ElementFlow control in which I have implemented a SelectionChanged event which (by definition) fires up a specific event when the control's item selection has changed.

我有一个 ElementFlow 控件,在其中我实现了一个 SelectionChanged 事件,该事件(根据定义)在控件的项目选择更改时触发特定事件。

What I would like it to do is:

我希望它做的是:

  1. Start a timer
  2. If the timer reaches 2 seconds then launch a MessageBox saying ("Hi there") for example
  3. If the selection changes before the timer reaches 2 seconds then the timer should be reset and started over again.
  1. 启动计时器
  2. 例如,如果计时器达到 2 秒,则启动一个 MessageBox 说(“Hi there”)
  3. 如果选择在计时器到达 2 秒之前发生变化,则应重置计时器并重新开始。

This is to ensure that the lengthy action only launches if the selection has not changed within 2 seconds but I am not familiar with the DispatcherTimer feature of WPF as i am more in the know when it comes to the normal Timer of Windows Forms.

这是为了确保只有在选择在 2 秒内没有更改的情况下才会启动冗长的操作,但我不熟悉 WPF 的 DispatcherTimer 功能,因为我更了解 Windows 窗体的正常计时器。

Thanks,

谢谢,

S.

S。

回答by Sheridan

Try this:

尝试这个:

private int timerTickCount = 0;
private bool hasSelectionChanged = false;
private DispatcherTimer timer;

In your constructor or relevant method:

在您的构造函数或相关方法中:

timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1); // will 'tick' once every second
timer.Tick += new EventHandler(Timer_Tick);
timer.Start();

And then an event handler:

然后是一个事件处理程序:

private void Timer_Tick(object sender, EventArgs e)
{
    DispatcherTimer timer = (DispatcherTimer)sender;
    if (++timerTickCount == 2)
    {
        if (hasSelectionChanged) timer.Stop();
        else MessageBox.Show("Hi there");
    }
}

Finally, to make this work, you just need to set the hasSelectionChangedvariable when the selection has changed according to your SelectionChangedevent.

最后,要完成这项工作,您只需要hasSelectionChanged在选择根据您的SelectionChanged事件发生变化时设置变量。

回答by touyets

I've figured the complete code out as such:

我已经想出了完整的代码:

DispatcherTimer _timer;

public MainWindow()
{
    _myTimer = new DispatcherTimer();
    _myTimer.Tick += MyTimerTick;
    _myTimer.Interval = new TimeSpan(0,0,0,1);
}

private void ElementFlowSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    _counter = 0;
    _myTimer.Stop();
    _myTimer.Interval = new TimeSpan(0, 0, 0, 1);
    _myTimer.Start();
}

private int _counter;
public int Counter
{
    get { return _counter; }
    set
        {
            _counter = value;
            OnPropertyChanged("Counter");
        }
}
private void MyTimerTick(object sender, EventArgs e)
{
    Counter++;
    if (Counter == 2)
    {
        _myTimer.Stop();
        MessageBox.Show(“Reached the 2 second countdown”);
    }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler e = PropertyChanged;
    if (e != null)
        {
            e(this, new PropertyChangedEventArgs(propertyName));
}
}

回答by loop

look here is the code of how to use DispatherTimer and you can add your own logic in it. that will depends on you..

看看这里是如何使用 DispatherTimer 的代码,您可以在其中添加自己的逻辑。这将取决于你..

 private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(2000);
        timer.Tick += timer_Tick;
        timer.Start();

    }

    void  timer_Tick(object sender, object e)
    {
       // show your message here..
    }

回答by Zenchovey

To use a DispatcherTimer:

要使用 DispatcherTimer:

    private DispatcherTimer _timer;
    public void StartTimer()
    {
        if (_timer == null)
        {
            _timer = new DispatcherTimer();
            _timer.Tick += _timer_Tick;
        }

        _timer.Interval = TimeSpan.FromSeconds(2);
        _timer.Start();
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("Hi there");
        _timer.Stop();
    }

    void SelectionChangedEvent()
    {
        StartTimer();
    }