高CPU使用率,移动光标应用程序C#

时间:2012-02-10 23:50:39

标签: c# memory cursor mouse cpu-usage

我正在开发应用程序,在某个区域移动鼠标,如果某些东西不是黑色,请单击鼠标。但是,我使用此方法获得了非常高的CPU使用率,也获得了光标下方的颜色。从startY到endY完成5次运行后 - 应用程序滞后了大约5-10秒。太过分了。随着这部分的注释,应用程序运行良好,每次运行都不会过于完整。

这是我的while循环:

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

光标下方的颜色:

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

如何最大限度地减少CPU的大量使用。

解决方案: 这是我的方法" Get"这导致了问题。我通过下面的方法解决了这个问题,然后在背景工作者中运行整个事情。

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 个答案:

答案 0 :(得分:1)

尝试将逻辑移动到这样的调用调用

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