Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:57:46+00:00 2026-05-29T15:57:46+00:00

I’m working on an app, moving the mouse in a certain area and clicking

  • 0

I’m working on an app, moving the mouse in a certain area and clicking the mouse if something is not the color black.

However, I am getting a really high CPU usage while using this method too get the color below the cursor. After 5 completed runs from startY to endY – the application is lagging that much it takes around 5-10 sec. to get too the end of the area. With this part commented out, the application runs fine and each run doesn’t increase in the too complete.

Here is my while loop:

private void moveMouse(int startX, int endX, int startY, int endY)
    {
        int newPosX = startX;
        int newPosY = startY;
        while (running)
        {
            Application.DoEvents();
            //this.Cursor = new Cursor(Cursor.Current.Handle);
            Cursor.Position = new Point(newPosX, newPosY);
            Thread.Sleep(3);
            if (colorCursor.Get(newPosX, newPosY))
            {
                MyMouse.sendClick();
                countClicks++;
                lblStatus.Text = "Klik: " + countClicks;
            }

            newPosX += 10;
            if (newPosX > endX)
            {
                newPosY += 25;
                newPosX = startX;
            }
            if (newPosY > endY)
            {
                newPosY = startY;
                Thread.Sleep(1000);
            }
        }
    }

Color below cursor:

public class ColorUnderCursor
{
    [DllImport("gdi32")]
    public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);

    //[DllImport("user32.dll", CharSet = CharSet.Auto)]
    //public static extern bool GetCursorPos(out POINT pt);

    [DllImport("User32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetWindowDC(IntPtr hWnd);

    public bool Get(int x, int y)
    {
        IntPtr dc = GetWindowDC(IntPtr.Zero);

        long color = GetPixel(dc, x, y);
        Color underMouse = Color.FromArgb((int)color);
        if(underMouse != Color.FromArgb(0, 0, 0, 0))
            return true;

        return false;
    }
}

How I can minimize this heavy usage of the CPU.

Solution:
It was my method "Get" which was causing the problem. I solved it by this method below, and running the whole thing inside a backgroundworker.

public bool GetPixel(Point position)
    {
        using (var bitmap = new Bitmap(1, 1))
        {
            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(position, new Point(0, 0), new Size(1, 1));
            }
            if (bitmap.GetPixel(0, 0) != Color.FromArgb(255, 0, 0, 0) && bitmap.GetPixel(0, 0) != Color.FromArgb(255, 255, 255, 255))
                return true;

            return false;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:57:48+00:00Added an answer on May 29, 2026 at 3:57 pm

    Try to move logic into Invoke call like this

    private void moveMouse(int startX, int endX, int startY, int endY)
    {
    
      this.BeginInvoke(new Action(() => { InvokeMouseMove(startX, endX, startY, endY)
       }));
    }
    
    private void InvokeMouseMove(int startX, int endX, int startY, int endY)
        {
            int newPosX = startX;
            int newPosY = startY;
            while (running)
            {
                Application.DoEvents();
                //this.Cursor = new Cursor(Cursor.Current.Handle);
                Cursor.Position = new Point(newPosX, newPosY);
    
                if (colorCursor.Get(newPosX, newPosY))
                {
                    MyMouse.sendClick();
                    countClicks++;
                    lblStatus.Text = "Klik: " + countClicks;
                }
    
                newPosX += 10;
                if (newPosX > endX)
                {
                    newPosY += 25;
                    newPosX = startX;
                }
                if (newPosY > endY)
                {
                    newPosY = startY;
                }
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.