定时器间隔,使1个定时器停止另一个定时器

时间:2009-10-01 17:58:56

标签: c# timer

嘿伙计们试图让一个简单的按钮捣碎器,我想让timer1做的是在richtextbox1中反复使用mash键30秒,30秒后激活timer2,这将禁用计时器1并按下richtextbox 2中的键一次,然后等待10秒再次激活计时器1。

我是极端新的c#但我已经尝试使用计时器3来停止计时器2并再次启动计时器1它只是自我混乱。我试过的代码如下。任何有助于帮助的人......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            SendKeys.Send(richTextBox1.Text);
            SendKeys.Send("{ENTER}");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            SendKeys.Send(richTextBox2.Text);
            SendKeys.Send("{ENTER}"); 
            timer1.Enabled = false;
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer2.Enabled = false;
        }

        private void richTextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果timer3连续运行,它会不会启动timer1并在不可预知的时间停止timer2而不会发出警告?

IOW,启动和停止计时器3是什么?

JustLoren指出,可能有一种更清洁的方法。也许是单个计时器事件和一些控制逻辑和标志,而不是试图兼顾三个计时器。

答案 1 :(得分:1)

我建议只使用一个计时器,每秒递增一个状态计数器,并根据当前状态执行操作。

public Form1()
{
    this.InitializeComponent();

    // Just to illustrate - can be done in the designer.
    this.timer.Interval = 1000; // One second.
    this.timer.Enable = true;
}

private Int32 state = 0;

private void timer_Tick(Object sender, EventArgs e)
{
    if ((0 <= this.state) && (this.state < 30)) // Hit text box 1 30 times.
    {
        SendKeys.Send(this.richTextBox1.Text);
        SendKeys.Send("{ENTER}");
    }
    else if (this.state == 30) // Hit text box 2 once.
    {
        SendKeys.Send(this.richTextBox2.Text);
        SendKeys.Send("{ENTER}");
    }
    else if ((31 <= this.state) && (this.state < 40)) // Do nothing 9 times.
    {
        // Do nothing.
    }
    else
    {
        throw new InvalidOperationException(); // Unexpected state.
    }

    // Update state.
    this.state = (this.state + 1) % 40;
}

具有两个数字向上控制的变体。

public Form1()
{
    this.InitializeComponent();

    // Just to illustrate - can be done in the designer.
    this.timer.Interval = 1000; // One second.
    this.timer.Enable = true;
}

private Int32 state = 0;

private void timer_Tick(Object sender, EventArgs e)
{
    Decimal n1 = this.numericUpDown1.Value;
    Decimal n2 = this.numericUpDown2.Value;

    if ((0 <= this.state) && (this.state < n1))
    {
        SendKeys.Send(this.richTextBox1.Text);
        SendKeys.Send("{ENTER}");
    }
    else if (this.state == n1)
    {
        SendKeys.Send(this.richTextBox2.Text);
        SendKeys.Send("{ENTER}");
    }
    else if ((n1 <= this.state) && (this.state < n1 + n2))
    {
        // Do nothing.
    }
    else
    {
        // Reset state to resolve race conditions.
        this.state = 0;
    }

    // Update state.
    this.state = (this.state + 1) % (n1 + n2);
}