是否可以减慢while循环?

时间:2018-01-24 08:11:12

标签: c#

重写:我正在编写一个程序来更好地理解C#并接受用户输入。现在我在C#中使用WFA和一个文本框,我把我想要粘贴的文本放在哪里/打出来。然后我有另外两个具有间隔输入和数量输入的框。一切都运行得很好,直到我添加了数量输入,我已经为用于键入/粘贴文本的计时器添加了一个while循环。但是,由于某种原因,我不确定..程序忽略了间隔的任何输入。

我基本上就像这样设置

private void timerPaste_Tick(object sender, EventArgs e)
    {

        interval = Int32.Parse(interNum.Text);
        timerPaste.Interval = interval;

        if (typeModebool == true && pasteModebool == false)
        {
            while (amount > 0) //Need to find a way to make GUI responsive during while loop.
            {
                SendKeys.Send(textBox1.Text);
                SendKeys.Send("{Enter}");
                amount--;
            }
            timerPaste.Enabled = false;
            amount = Int32.Parse(amountSetBox.Text);
        }

        if (pasteModebool == true && typeModebool == false)
        {
            while (amount > 0) //Need to find a way to make GUI responsive during while loop.
            {
                Clipboard.SetText(textBox1.Text);
                SendKeys.Send("^{c}");
                SendKeys.Send("^{v}");
                SendKeys.Send("{Enter}");
                amount--;
            }
            timerPaste.Enabled = false;
            amount = Int32.Parse(amountSetBox.Text);
        }
    }

2 个答案:

答案 0 :(得分:0)

那样的东西......?

  timerPaste.Elapsed += OnTickEvent;

    private async void OnTickEvent(Object source,EventArgs e)
    {
            interval = Int32.Parse(interNum.Text);
            timerPaste.Interval = interval;

            if (typeModebool == true && pasteModebool == false)
            {
                while (amount > 0) //Need to find a way to make GUI responsive during while loop.
                {
                    SendKeys.Send(textBox1.Text);
                    SendKeys.Send("{Enter}");
                    amount--;
                }
                timerPaste.Enabled = false;
                amount = Int32.Parse(amountSetBox.Text);
            }

            if (pasteModebool == true && typeModebool == false)
            {
                while (amount > 0) //Need to find a way to make GUI responsive during while loop.
                {
                    Clipboard.SetText(textBox1.Text);
                    SendKeys.Send("^{c}");
                    SendKeys.Send("^{v}");
                    SendKeys.Send("{Enter}");
                    amount--;
                }


         timerPaste.Enabled = false;
            amount = Int32.Parse(amountSetBox.Text);
        }
    }

答案 1 :(得分:0)

我实际上能解决这个问题,问题是while循环第一次听到了计时器,之后就开始关闭它自己了。我用if语句将其切换为if,并且现在一切正常。

int i = 0;

    private void timerPaste_Tick(object sender, EventArgs e)
    {
        if (typeModebool == true && pasteModebool == false) //Check what mode is being used
        {
            if (i < amount) //If runs until i == amount
            {
                SendKeys.Send(textBox1.Text);
                SendKeys.Send("{Enter}");
                amount--;
            }
            else
            {
                timerPaste.Enabled = false;
            }
        }

        if (pasteModebool == true && typeModebool == false)
        {
            if (i < amount)
            {
                Clipboard.SetText(textBox1.Text);
                SendKeys.Send("^{c}");
                SendKeys.Send("^{v}");
                SendKeys.Send("{Enter}");
                amount--;
            }
            else
            {
                timerPaste.Enabled = false;
            }
        }
    }