【问题标题】:Speeding up DispatcherTimer when button is clicked单击按钮时加快 DispatcherTimer
【发布时间】:2011-03-21 03:52:50
【问题描述】:

我正在创建一个练习应用程序,它每隔几秒播放一次声音,按下按钮会缩短播放声音的时间间隔。例如,在开始时它每 15 秒播放一次声音,当我按下按钮时,它现在应该每 10 秒播放一次声音。基本上每次用户按下按钮时,它都会减少 5 秒的间隔。因此,我在类的顶部有一个 DispatcherTimer,并在页面像这样加载时创建了它的间隔和 Tick 方法。我也有一个 int 变量用于时间跨度中的秒数。

DispatcherTimer timer = new DispatcherTimer();
int interval = 15;

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
        timer.Interval = new TimeSpan(0, 0, interval);
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();
}

void btn_Click(object sender, RoutedEventArgs e)
{
        Button btn = sender as Button;

        interval -= 5;

        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0, 0, interval);
}

但这会让声音播放得非常疯狂。有没有合适的方法来做到这一点?

【问题讨论】:

    标签: windows-phone-7


    【解决方案1】:

    如果这是您的实际代码,那么 Larry 是完全正确的... 您必须在开始第二个之前调用 timer.stop();。 还要给出一些条件以禁止interval < 0

    【讨论】:

      【解决方案2】:

      您是否在启动第二个调度程序计时器之前取消了第一个调度程序计时器?是否有可能在第一次单击后触发了 两个 调度程序计时器?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多