简体   繁体   English

调度程序中的时间跨度

[英]Timespan in a dispatcherTimer

I'm programing a WPF application in VS C# 2010 and I'm programming a simulation. 我正在VS C#2010中对WPF应用程序进行编程,并且正在对模拟进行编程。 This simulation can be either run automatically (by pressing the Auto button) or step by step (clicking the Step button). 可以自动运行此模拟(通过按“自动”按钮),也可以逐步运行(单击“步进”按钮)。 However, what I want to implement is a speed control. 但是,我要实现的是速度控制。

I have designed a simple comboBox with 4 possible items (1,2,5,10), which represent the simulation speed. 我设计了一个简单的comboBox,其中包含4个可能的项目(1、2、5、10),它们代表了模拟速度。 Here is the code I'm using: 这是我正在使用的代码:

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();
    }

What this is supposed to do is to take the value selected in the comboBox and since TimeSpan does not accept double, just Int32, I must use the 4th parameter, miliseconds. 这应该做的就是获取在comboBox中选择的值,并且由于TimeSpan不接受double,仅接受Int32,所以我必须使用第4个参数,即毫秒。 I thought that doing 1000/speed would work but it does absolutely not, the time is even bigger. 我认为以1000 /速度进行操作是可以的,但绝对不能,时间会更长。 How can I change the time interval, for example, to reduce it from just 1 second (default at x1 Speed) to every 200 ms when the user selects the x5 Option? 当用户选择“ x5选件”时,如何更改时间间隔,例如将其从1秒(默认为x1 Speed)减少到每200 ms?

You are using a wrong overload of TimeSpan , the fourth argument is actually for second (the first is for day ). 您使用了错误的TimeSpan重载,第四个参数实际上是second (第一个参数是day )。 Try this simple static method TimeSpan.FromMilliseconds instead: 请尝试使用以下简单的静态方法TimeSpan.FromMilliseconds

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM