title: C# mouse events on autoclicker failing randomly
body: <p>I'm writing a small program to automate clicks on a browser game. Using the code bellow the mouse always moves to the right location, however the click on the button only works about 50% of the times. Why does it not work every time and how do i fix it?</p>
  
  <p>EDIT: Here is the updated code (2 methods about image recognition left in blank due to code block size restrictions):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  
  namespace CICautoplayer
  {
  
  public partial class CICplayer : Form
  {
      public CICplayer()
      {
          InitializeComponent();
      }
  
      [DllImport("user32.dll", EntryPoint = "SetCursorPos")]
      private static extern bool SetCursorPos(int x, int y);
  
      [DllImport("user32.dll")]
      public static extern void mouse_event(int dwflags, int dx, int dy, int cButtons, int dwExtraInfo);
  
      private const int MOUSEEVENTF_LEFTDOWN = 0x02;
      private const int MOUSEEVENTF_LEFTUP = 0x04;
      Random rnd = new Random();
  
      bool run = false;
      int[] corner = new int[] { 0, 0 };
      int[] pos = new int[] { 0, 0 };
      int level = 0;
      string[] logbook = new string[9] { " ", " ", " ", " ", " ", " ", " ", " ", " " };
  
      private void runner()
      {
          while (run)
          {
              // =================================================================== identify level
  
              bool n1found = false;
              bool n2found = false;
              int n1 = 9;
              int n2 = 9;
              while (!n1found)
              {
                  string name = "n" + n1.ToString();
                  pos = new int[] { 0, 0 };
                  pos = position_of_level(name, corner[0] + 0, corner[0] + 15, corner[1] + 10, corner[1] + 40);
  
                  if (pos[0] &gt; 0 &amp;&amp; pos[1] &gt; 0) { n1found = true; }
                  else if (n1 &gt; 0) { n1--; }
                  else { n1found = true;}
              }
              while (!n2found)
              {
                  string name = "n" + n2.ToString();
                  pos = new int[] { 0, 0 };
                  pos = position_of_level(name, corner[0] + 15, corner[0] + 40, corner[1] + 10, corner[1] + 40);
  
                  if (pos[0] &gt; 0 &amp;&amp; pos[1] &gt; 0){ n2found = true; }
                  else if (n2 &gt; 0) { n2--; }
                  else { n2found = true;  n2 = 1000; }
              }
              level = n1 * 10 + n2;
              addentry("LVL " + level.ToString() + " =&gt; ");
  
              // ================================================================ target rewards
  
              if (level &gt; 5)
              {
                  click_on(new int[] { corner[0] + 20, corner[1] + 20 });
                  Thread.Sleep(1100 + rnd.Next(1, 100));
  
                  //string[] targetlist = new string[] { "saltpeter", "jewels", "tools", "magnificent", "steel", "bolts", "diamond", "lore"};
  
                  string[] targetlist = new string[] { "saltpeter", "jewels", "magnificent", "bolts", "diamond", "lore" };
  
                  int[] temp_pos = new int[] { 0, 0 };
                  temp_pos = position_of("corner_slider", corner[0] + 200, corner[0] + 250, corner[1] + 250, corner[1] + 550);
  
                  bool chosen = false;
                  int n = 0;
                  while (n &lt; targetlist.Count() &amp;&amp; !chosen)
                  {
                      pos = new int[] { 0, 0 };
                      pos = position_of(targetlist[n], temp_pos[0] + 200, temp_pos[0] + 550, temp_pos[1], temp_pos[1] + 50);
                      if (pos[0] != 0 &amp;&amp; pos[1] != 0)
                      {
                          click_on(new int[] { pos[0] + 10, pos[1] + 10 });
                          chosen = true;
                      }
                      n++;
                  }
  
                  if (chosen)
                  {
                      addentry("Rwrd: " + targetlist[n - 1] + " =&gt; ");
                  }
                  else
                  {
                      addentry("Rwrd: NONE! =&gt; ");
                  }
  
  
                  // fecher o menu das rewards
                  Thread.Sleep(1000 + rnd.Next(1, 100));
                  click_on(new int[] { corner[0] + 50, corner[1] + 400 });
              }
  
              // =============================================================== wait time
  
              int wtime = 0;
              if (level &lt; 30) { wtime = 4000 + level * 200;
              } else if (level &lt; 60)
              {
                  wtime = 10000 + level * 250;
              } else
              {
                  wtime = 25000;
              }
  
              addentry("Wait: " + (wtime/1000).ToString() + " sec =&gt; ");
              Thread.Sleep(wtime);
  
              // ================================================================ reset
              if (run) // condition in case the stop button is pressed meanwhile
              {
                  addentry("reseting... \r\n");
                  click_on(new int[] { corner[0] + 20, corner[1] + 20 });
                  Thread.Sleep(2000 + rnd.Next(1, 500));
  
                  // select restart
                  int[] p_but = new int[] { 0, 0 };
                  p_but = position_of("button_restart", corner[0] + 450, corner[0] + 550, corner[1] + 200, corner[1] + 400);
                  p_but[0] += 10; p_but[1] += 10;
                  click_on(p_but);
  
                  Thread.Sleep(4000 + rnd.Next(1, 500));
  
                  click_on(new int[] { corner[0] + 700, corner[1] + (level &lt; 6 ? 335 : 385)});
                  Thread.Sleep(5000);
              }
  
          }
  
      }
  
      public void click_on(int[] coor)
      {
          SetCursorPos(coor[0], coor[1]);
          Application.DoEvents();
          mouse_event(MOUSEEVENTF_LEFTDOWN, coor[0], coor[1], 0, 0);
          mouse_event(MOUSEEVENTF_LEFTUP, coor[0], coor[1], 0, 0);
      }
  
      private void addentry(string text)
      {
          if (InvokeRequired)
          {
              this.Invoke(new Action&lt;string&gt;(addentry), new object[] { text });
              return;
          }
  
          for (int i = 0; i &lt; 8; i++) { logbook[i] = logbook[i + 1]; }
          logbook[8] = text;
  
          logtext.Text = "";
          for (int i = 0; i &lt; 9; i++)
          {
              logtext.Text += logbook[i];
          }
      }
  
      private int[] position_of(string what, int xmin, int xmax, int ymin, int ymax)
      {
          // code to identify a picture in a specified area of the screen
          // returns int[2] with the coordinates
      }
  
      private int[] position_of_level(string what, int xmin, int xmax, int ymin, int ymax)
      {
          // code to identify a number in a specified area of the screen
          // returns int[2] with the coordinates
      }
  
      private void but_play_Click(object sender, EventArgs e)
      {
          Thread.Sleep(3000);
          corner = position_of("corner", 0, 500, 0, 600);
          addentry("Corner found at " + corner[0].ToString() + " , " + corner[1].ToString() + "\r\n");
          run = true;
          var thread = new Thread(new ThreadStart(runner));
          thread.Start();
      }
      private void but_stop_Click(object sender, EventArgs e)
      {
          run = false;
      }
  
  }
  }
  </code></pre>