tags:

views:

369

answers:

2

Hey guys im trying to get a simple button masher up, What i want timer1 to do is mash keys in richtextbox1 for 30 seconds over and over, after 30 seconds Activate timer2, which will disable timer 1 and press keys in richtextbox 2 once, then wait 10 seconds and activate timer 1 again.

Im extremley new to c# but ive tried using timer 3 to stop timer 2 and start timer 1 again and it just messes it self up. The code ive tried is below. Any help apreciated...

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)
        {

        }
    }
}
+1  A: 

If timer3 is running continuously, won't it start timer1 and stop timer2 at unpredictable times, without warning?

IOW, what starts and stops timer3?

As JustLoren pointed out, there might be a cleaner way to do this. Perhaps a single timer event and some controlling logic and flags, rather than trying to juggle three timers.

Robert Harvey
Yeah, using a lot of timers is usually a sign that the code can be simplified greatly.
Charlie Salts
+1  A: 

I suggest to use just one timer, increment a state counter every second, and perform an action base on the current state.

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

The variant with two numeric up down controls.

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);
}
Daniel Brückner
Hey thanks for that, it works almost the way i want it to. Using the method above it hits the keys in richtexbox1 only once, then waits 30 seconds and hits keys in textbox2. i need it to hit keys from textbox1 over and over for 30 seconds, what would i add in there to do this ?
Simar
What frequency do you want during the 30 seconds?
Daniel Brückner
every 1 second should be fine. But it has to be only for the text in textbox 1.
Simar
I still have no clue what you want to achieve, but I can tell you how to do it ... :D Going to update my answer.
Daniel Brückner
Hey mate thats pretty much exactly what i was looking for! works pretty good now!, dont want to push my luck but any chance you can point me in the right direct to see how i can add a texbox so i can type the interval in there and run it? so that the 30 seconds and 10 seconds can be changed on the fly ?if this is possible?
Simar
Don't use text boxes, use two NumericUpDownControls. Replace (this.state < 30) and (this.state == 30) with numericUpDown1.Value, (31 <= this.state) with numericUpDown1.Value + 1, and (this.state < 40) and (this.state + 1) % 40 with numericUpDown1.Value + numericUpDown2.Value. Instead of throwing the exception, you should set this.state to zero to avoid race conditions.
Daniel Brückner
Hey thanks. This is what ive done, Does it look correct. if ((0 <= this.state) Do i delete " else if (this.state == 30) // Hit text box 2 once. "
Simar
I also have 3 errors. #1 : Operator '
Simar
Hey man thanks alot!, I changed it and i get 1 error now. //// Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?) /// This comes up @ // Update state. this.state = (this.state + 1) % (n1 + n2);Sorry to be such a hassle! thanks again for the help
Simar
oh also i tried removing the update state section that causes the error, it gets rid of the error but then what ever i put into textbox2 is ignored and only textbox 1 is pressed.
Simar
Just add an explicit cast to the state update ... this.state = (this.state + 1) % (Int32)(n1 + n2);
Daniel Brückner