Timespan in dispatcherTimer

时间:2015-10-17 21:45:30

标签: c# wpf timer timespan dispatchertimer

我正在VS C#2010中编写WPF应用程序,我正在编程模拟。此模拟可以自动运行(通过按“自动”按钮)或逐步运行(单击“步骤”按钮)。但是,我想要实现的是速度控制。

我设计了一个带有4个可能项目(1,2,5,10)的简单组合框,它代表了模拟速度。这是我正在使用的代码:

private void button6_Click(object sender, EventArgs e)
    {
        int speed = Int32.Parse(comboBox1.Text.ToString());
        dispathcerTimer = new DispatcherTimer();
        dispathcerTimer.Tick +=new EventHandler(dispatcherTimer_Tick);
        dispathcerTimer.Interval = new TimeSpan(0, 0, 0, Convert.ToInt32(1000/speed));
        dispathcerTimer.Start();
    }

这应该做的是获取在comboBox中选择的值,因为TimeSpan不接受double,只需要Int32,我必须使用第4个参数,毫秒。我认为1000 /速度会起作用,但绝对没有,时间更长。如何更改时间间隔,例如,当用户选择x5选项时,将其从1秒(默认为x1速度)减少到每200毫秒?

1 个答案:

答案 0 :(得分:0)

您正在使用TimeSpan的错误重载,第四个参数实际上是second(第一个是day)。请尝试使用这种简单的静态方法TimeSpan.FromMilliseconds

dispathcerTimer.Interval = TimeSpan.FromMilliseconds(1000/speed);
相关问题