简体   繁体   English

C#计时器,我可以加快但不能减慢速度

[英]C# timer I can speed up but not slow down

I have a timer event setup and I would like to change how often the timer event happens by reading a number from a text box. 我有一个计时器事件设置,我想通过从文本框中读取一个数字来更改计时器事件发生的频率。 If the box is '10' and you click the update button the event would trigger every 10ms then if you changed to '100' and clicked it would happen every 100ms and so on. 如果该框为“ 10”,然后单击“更新”按钮,则事件将每10ms触发一次;然后,如果更改为“ 100”并单击,则事件将每100ms触发一次,依此类推。

When I run the program however, i can speed up the event frequency (eg 100ms to 10ms) but I cannot slow it down (eg 10ms to 100ms). 但是,当我运行程序时,我可以加快事件频率(例如100ms至10ms),但不能降低事件频率(例如10ms至100ms)。 Here is the piece of my code that changes the timer when I click: 这是我单击时更改计时器的代码片段:

    private void TimerButton_Click(object sender, EventArgs e)
    {

        getTime = ImgTimeInterval.Text;
        bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
        label2.Text = isNumeric.ToString();
        if (isNumeric)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Interval = timerMS;
            timer.Elapsed += new ElapsedEventHandler(timerEvent);
            timer.AutoReset = true;
            timer.Enabled = true;
        }
    }

    public void timerEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        label1.Text = counter.ToString();
        counter = (counter + 1) % 100;
    }

If anyone knows what I may be doing wrong it would be greatly appreciated. 如果有人知道我在做什么错,将不胜感激。

The problem with this code is, that you create a new Timer each time you click the button. 此代码的问题是,每次单击按钮时,您都会创建一个新的Timer Try to create the timer outside the method. 尝试在方法之外创建计时器。 You think it's only goes faster, but instead multiple timers trigger the timerEvent 您认为它只会运行得更快,而是多个计时器触发了timerEvent

private System.Timers.Timer _timer;

private void CreateTimer()
{
    _timer = new System.Timers.Timer();
    _timer.Enabled = false;
    _timer.Interval = 100;  // default
    _timer.Elapsed += new ElapsedEventHandler(timerEvent);
    _timer.AutoReset = true;
    _timer.Enabled = true;    
}

private void TimerButton_Click(object sender, EventArgs e)
{
    bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
    label2.Text = isNumeric.ToString();
    if (isNumeric)
    {
        _timer.Interval = timerMS;
    }
}

public void timerEvent(object source, System.Timers.ElapsedEventArgs e)
{
    label1.Text = counter.ToString();
    counter = (counter + 1) % 100;
}

Make sure that the CreateTimer is called in the constructor/formload. 确保CreateTimer被称为在构造函数/ formload。 Also you can now stop the timer within another button event. 您现在也可以在另一个按钮事件中停止计时器。 With _timer.Enabled = false; 使用_timer.Enabled = false;

You're always creating a new timer and never stopping the old timer. 您总是在创建一个新计时器,而从不停止旧计时器。 When you "change" it from 100 to 10 your 100ms timer is still firing every 100 ms, so every 100ms two timers are firing at around the same time. 当您将其从100更改为10时,您的100毫秒计时器仍然每100毫秒触发一次,因此每100毫秒两个计时器几乎同时触发。

You need to "remember" the old timer so that you can stop it. 您需要“记住”旧计时器,以便将其停止。 Or, better yet, just have only one timer that you change the interval on. 或者,更好的是,只有一个定时器可以更改间隔。

private System.Timers.Timer timer = new System.Timers.Timer();
public Form1()
{
    timer.Enabled = false;
    timer.AutoReset = true;
    timer.Elapsed += timerEvent;
}

private void TimerButton_Click(object sender, EventArgs e)
{
    getTime = ImgTimeInterval.Text;
    bool isNumeric = int.TryParse(ImgTimeInterval.Text, out timerMS); //if number place number in timerMS
    label2.Text = isNumeric.ToString();
    if (isNumeric)
    {
        timer.Interval = timerMS;
        timer.Enabled = true;
    }
}

Well the basic problem is that you're building a new one every time. 好吧,基本问题是您每次都在构建一个新的。 Make a private timer: 设置一个私人计时器:

private System.Timers.Timer _timer = new System.Timers.Timer();

and then fix it up when the button is clicked: 然后在单击按钮时对其进行修复:

if (isNumeric)
{
    _timer.Stop();
    _timer.Interval = timerMS;
    _timer.Start();
}

and then in the .ctor , do this: 然后在.ctor ,执行以下操作:

_timer.Elapsed += new ElapsedEventHandler(timerEvent);

Now you have a single timer that you are just modifying as the user changes the value in the text box. 现在,只有一个计时器,您可以在用户更改文本框中的值时对其进行修改。

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

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