Simar
Simar

Reputation:

Timer intervals, Making 1 timer stop another timer -

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

        }
    }
}

Upvotes: 1

Views: 2173

Answers (2)

Daniel Brückner
Daniel Brückner

Reputation: 59705

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

Upvotes: 1

Robert Harvey
Robert Harvey

Reputation: 180908

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.

Upvotes: 1

Related Questions