dispatcherTimer中的时间跨度

本文关键字:时间跨度 dispatcherTimer | 更新日期: 2023-09-27 18:18:13

我正在VS c# 2010中编程WPF应用程序,我正在编程模拟。这个模拟可以自动运行(通过按Auto按钮),也可以一步一步运行(单击step按钮)。然而,我想实现的是一个速度控制。

我设计了一个简单的comboBox,有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();
    }

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

dispatcherTimer中的时间跨度

您使用了错误的TimeSpan过载,第四个参数实际上是用于second(第一个是用于day)。试试这个简单的静态方法TimeSpan.FromMilliseconds:

dispathcerTimer.Interval = TimeSpan.FromMilliseconds(1000/speed);