-6

Rewrite: I'm writing a program to get a better understanding of C# and taking user input in. Right now I'm using WFA in C# with a textbox where I put the text of what I want pasted/typed out. Then I have two other boxes that have an interval input and a amount input. Everything worked perfectly fine until I added the amount input, I've added a while loop to a timer that is used to type/paste the text. However, for some reason that I'm not sure of.. The program disregards any input for the interval.

I have it basically setup like this

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);
        }
    }
Expenox
  • 11
  • 2
  • 2
    Why would you do that? What is inside the `Code here` segment? – Patrick Hofman Jan 24 '18 at 08:12
  • `System.Threading.Thread.Sleep(1000)`? – waka Jan 24 '18 at 08:12
  • 1
    set a timer dude... – Leon Barkan Jan 24 '18 at 08:14
  • I'm trying to see what I can control as I'm trying to get back into c# after a while. I've researched before posting otherwise I wouldn't have made an account.. I've tried setting the two variables for user inputs as async so I could maybe delay the actual loop while in it.. It didn't work. As for making the thread sleep, I've read that would make the application unresponsive while working with that piece of code.. which isn't something that I'd find ideal. I wasn't sure how to go about it using a timer. – Expenox Jan 24 '18 at 13:33
  • @LeonBarkan Thank you, I've figured out how to do it and it now works. I appreciate the help, sorry for a noob question. – Expenox Jan 24 '18 at 14:26
  • @Expenox it's ok the experience will come with the time – Leon Barkan Jan 24 '18 at 14:45
  • https://stackoverflow.com/questions/12535722/what-is-the-best-way-to-implement-a-timer – Leon Barkan Jan 24 '18 at 14:46
  • @LeonBarkan I updated my post with another issue, have any idea? Also, is the way I rewrote the post better than before? – Expenox Jan 24 '18 at 17:17
  • 1
    If you have a new problem you should post a new question. – Dour High Arch Jan 24 '18 at 20:09
  • @DourHighArch I can't because I got so much negative feedback on my original question... When I can, if I still have the issue in 5 days I will. Thank you. – Expenox Jan 24 '18 at 21:25
  • throw it in a different thread – Leon Barkan Jan 25 '18 at 07:19
  • or make an async Event – Leon Barkan Jan 25 '18 at 07:26

2 Answers2

0

something like that..?

  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);
        }
    }
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43
  • For the timerPaste.Elapsed += OnTickEvent; .Elapsed shows this: http://prntscr.com/i5qboa I've already done some research and tried changing EventArgs to ElapsedEventArgs as per someone else's post.. It still shows the same thing – Expenox Jan 26 '18 at 03:50
0

I actually was able to fix this, the problem was that the while loop listened to the timer the first time, then after that just started going off on it's own. I switched it to a if, with an else statement and everything works as intended now.

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;
            }
        }
    }
Expenox
  • 11
  • 2