-1

I've made this program in C#:

namespace Spammer
{
    public partial class Form1 : Form
    {
        int delay, y = 1;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            delay = int.Parse(textBox2.Text);
            timer1.Interval = delay;
            timer1.Enabled = true;
        }

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

        private void timer1_Tick(object sender, EventArgs e)
        {
                String textt = textBox1.Text;
                SendKeys.SendWait(textt);
        }
    }
}

It works fine most of the time, and it can really send keys quickly.

But when I insert a delay of, for example, 10 MS, it's very hard to click the "Stop" button to stop it. The only way to stop the sending is to close the program and I don't want to do that.

Is there anyway I can send keys very quickly, like 5-10 MS, without it impairing my ability to press the buttons inside the program? I can't click while it's sending quickly...

LarsTech
  • 80,625
  • 14
  • 153
  • 225
BlueRay101
  • 1,447
  • 2
  • 18
  • 29

3 Answers3

3

The problem is that you're using SendWait. That will wait for the target application to respond - and while that's happening, your application won't be able to respond to user input. If you use Send instead of SendWait, your UI thread won't be blocked waiting for the key press to be processed.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thank you, but sadly it doesn't work. I've changed it from SendWait to Send, but it still does the same thing. The buttons in the program cannot be pressed, whenever I click them nothing happens (they only have that blue thing around them). I just can't click them, and even when I'm clicking them a lot of times, nothing happens. It keeps sending keys even though it's now Send and not SendWait. What can I do? – BlueRay101 Oct 28 '13 at 15:57
  • 1
    @BarSharabani: What do you mean by "that blue thing"? It's really not clear what's going on, to be honest. – Jon Skeet Oct 28 '13 at 16:06
  • When you hover the mouse over the buttons, a blue frame around them appears (at least on Win7/Win8). So when I try to click them that blue frame appears and it's impossible to click them. I just can't click them no matter what I do. – BlueRay101 Oct 28 '13 at 16:11
1

I was able to reproduce the issue. The app is sending a keystroke every 10 milliseconds. To me, this is not at all surprising that the app is causing freezes. A keystroke every 10 milliseconds is quite a barrage to the active App. Threading is not going to help. Why is this behavior surprising?

In other words, I don't expect things to work out well when I overload the message pump.

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Spammer//your own namesapce
{
    public partial class Form1 : Form
    {
        int delayInMilliseconds, y = 1;
        private Timer timer1;

        public Form1()
        {
            InitializeComponent();
            //StartTimerWithThreading();       
            SetupTimer();
        }

        void StartTimerWithThreading()
        {
            Task.Factory.StartNew(() =>
                {
                    SetupTimer();
                });
        }

        void SetupTimer()
        {
            timer1 = new Timer();//Assume system.windows.forms.timer
            textBox2.Text = "10";//new delay
            timer1.Tick += timer1_Tick;//handler
        }

        private void button1_Click(object sender, EventArgs e)
        {
            delayInMilliseconds = int.Parse(textBox2.Text);
            timer1.Interval = delayInMilliseconds;
            timer1.Enabled = true;
        }

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

        private void timer1_Tick(object sender, EventArgs e)
        {
            String textt = textBox1.Text;
            SendKeys.SendWait(textt);
        }

    }
}
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • I've changed the SendWait to Send, but it still doesn't work. So what should I do? Just create a separate thread? – BlueRay101 Oct 28 '13 at 16:00
  • Task.Factory.StartNew is an easy way to do it. If u prefer I can write something more specific in a few minutes – P.Brian.Mackey Oct 28 '13 at 16:05
  • Yes Brian, if you could do that, please, it'd be very nice. I've now tried to run the SendKeys on a separate thread with no success. I mean, I've made the thread and all, but using the timer from the UI to work with the second thread is problematic and I failed doing it. – BlueRay101 Oct 28 '13 at 16:06
  • Thanks, there's just one small issue: "Error 1 'Spammer.Form1.Dispose(bool)': no suitable method found to override" – BlueRay101 Oct 28 '13 at 16:40
  • @BarSharabani - If you copied the code above be sure to change the namespace back to your own. Otherwise the designer generated code will break. – P.Brian.Mackey Oct 28 '13 at 16:42
0

The simple solution is instead of adding code to a Click event handler for your button, we need a MouseDown event handler:

//MouseDown event handler for the button2
private void button2_MouseDown(object sender, EventArgs e) {
   timer1.Enabled = false;
}

Or you can keep using the Click event handler but we send the key only when the MouseButtons is not Left like this:

private void timer1_Tick(object sender, EventArgs e) {
   String textt = textBox1.Text;
   if(MouseButtons != MouseButtons.Left) SendKeys.Send(textt);
}
//then you can freely click your button to stop it.
King King
  • 61,710
  • 16
  • 105
  • 130