繁体   English   中英

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

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

我有一个计时器事件设置,我想通过从文本框中读取一个数字来更改计时器事件发生的频率。 如果该框为“ 10”,然后单击“更新”按钮,则事件将每10ms触发一次;然后,如果更改为“ 100”并单击,则事件将每100ms触发一次,依此类推。

但是,当我运行程序时,我可以加快事件频率(例如100ms至10ms),但不能降低事件频率(例如10ms至100ms)。 这是我单击时更改计时器的代码片段:

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

如果有人知道我在做什么错,将不胜感激。

此代码的问题是,每次单击按钮时,您都会创建一个新的Timer 尝试在方法之外创建计时器。 您认为它只会运行得更快,而是多个计时器触发了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;
}

确保CreateTimer被称为在构造函数/ formload。 您现在也可以在另一个按钮事件中停止计时器。 使用_timer.Enabled = false;

您总是在创建一个新计时器,而从不停止旧计时器。 当您将其从100更改为10时,您的100毫秒计时器仍然每100毫秒触发一次,因此每100毫秒两个计时器几乎同时触发。

您需要“记住”旧计时器,以便将其停止。 或者,更好的是,只有一个定时器可以更改间隔。

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

好吧,基本问题是您每次都在构建一个新的。 设置一个私人计时器:

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

然后在单击按钮时对其进行修复:

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

然后在.ctor ,执行以下操作:

_timer.Elapsed += new ElapsedEventHandler(timerEvent);

现在,只有一个计时器,您可以在用户更改文本框中的值时对其进行修改。

暂无
暂无

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

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