繁体   English   中英

以随机间隔使用计时器

[英]Use timer with random interval

我正在尝试通过使用下面的代码在随机间隔中运行计时器。 问题是,当我这样做时,我只会发布1个随机数,而且不确定如何使用我的代码获取下一个随机数:

using System;
using System.Windows.Forms;

namespace Auto_Typer
{
    public partial class AutoTyper : Form
    {
        public AutoTyper()
        {
            InitializeComponent();

            tmrInterval.Tick += new EventHandler(Interval);

            txtInterval.TextChanged += new EventHandler(TextChanged);
            txtMin.TextChanged += new EventHandler(TextChanged);
            txtMax.TextChanged += new EventHandler(TextChanged);
        }

        private void TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(txtInterval.Text) < 1 || int.Parse(txtMin.Text) < 1 || int.Parse(txtMax.Text) < 1)
            {
                txtInterval.Text = "1";
                txtMin.Text = "1";
                txtMax.Text = "1";
            }
            else if (int.Parse(txtInterval.Text) > 100)
            {
                txtInterval.Text = "100";
                txtMin.Text = "100";
                txtMax.Text = "100";
            }
            else if (int.Parse(txtMin.Text) >= int.Parse(txtMax.Text))
            {
                txtMax.Text = (int.Parse(txtMin.Text) + 1).ToString();
            }
        }

        void Interval(object sender, EventArgs e)
        {
            SendKeys.Send(txtText.Text + ", " + tmrInterval.Interval + "{enter}");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (tbType.SelectedTab == tbInterval)
            {
                tmrInterval.Interval = int.Parse(txtInterval.Text) * 1000;
                tmrInterval.Enabled = true;
            }

            if (tbType.SelectedTab == tbRange)
            {
                Random random = new Random();

                tmrInterval.Interval = (random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
                tmrInterval.Enabled = true;
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            tmrInterval.Enabled = false;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

我建议将“ new Random()”调用移至初始化程序,然后在每次需要新的随机数时简单地调用Random.Next()。

有关Random()的更多信息,请访问: http : //msdn.microsoft.com/zh-cn/library/h343ddh9.aspx

随机并不是真正的随机,而是伪随机。 这意味着给定相同的起始种子(为Random实例提供的参数),随后对Random.Next()的调用将每次返回相同的值序列。 好消息(感谢phoog)是默认构造函数(无args)自动使用基于时间的种子。

只需将回调方法中的间隔更改为一个随机更改的间隔:

private Random random = new Random();

void Interval(object sender, EventArgs e)
{
    tmrInterval.Interval = (random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
    SendKeys.Send(txtText.Text + ", " + tmrInterval.Interval + "{enter}");
}

并且(如上所述)将Random实例设置为类的成员字段,因此您不必每次都进行更新(如果以非常快的顺序调用可能会带来问题)。

现在,每次单击按钮都会创建Random实例,因为它是处理程序的本地变量。 要在印刷机之间持久保存它,您需要使其成为一个字段。 --

private Random _random = new Random();

现在,点击处理程序可以从字段中获取下一个随机值

if (tbType.SelectedTab == tbRange)
{
    tmrInterval.Interval = (_random.Next(int.Parse(txtMin.Text), int.Parse(txtMax.Text)) * 1000);
    tmrInterval.Enabled = true;
}

暂无
暂无

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

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