dispatcherTimer 中的时间跨度
Timespan in a dispatcherTimer
我正在 VS C# 2010 中编写 WPF 应用程序,并且正在编写模拟程序。此模拟可以是 运行 自动(通过按“自动”按钮)或逐步(单击“步骤”按钮)。但是,我想要实现的是速度控制。
我设计了一个简单的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 不接受双精度,只接受 Int32,我必须使用第 4 个参数,毫秒。我认为做 1000/speed 会起作用,但它绝对不会,时间甚至更大。如何更改时间间隔,例如,当用户选择 x5 选项时,将其从仅 1 秒(默认为 x1 速度)减少到每 200 毫秒?
您使用了 TimeSpan
的错误重载,第四个参数实际上是 second
(第一个是 day
)。试试这个简单的静态方法 TimeSpan.FromMilliseconds
代替:
dispathcerTimer.Interval = TimeSpan.FromMilliseconds(1000/speed);
我正在 VS C# 2010 中编写 WPF 应用程序,并且正在编写模拟程序。此模拟可以是 运行 自动(通过按“自动”按钮)或逐步(单击“步骤”按钮)。但是,我想要实现的是速度控制。
我设计了一个简单的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 不接受双精度,只接受 Int32,我必须使用第 4 个参数,毫秒。我认为做 1000/speed 会起作用,但它绝对不会,时间甚至更大。如何更改时间间隔,例如,当用户选择 x5 选项时,将其从仅 1 秒(默认为 x1 速度)减少到每 200 毫秒?
您使用了 TimeSpan
的错误重载,第四个参数实际上是 second
(第一个是 day
)。试试这个简单的静态方法 TimeSpan.FromMilliseconds
代替:
dispathcerTimer.Interval = TimeSpan.FromMilliseconds(1000/speed);