功能导致屏幕闪现冻结

发布于 2024-11-25 22:19:36 字数 4630 浏览 0 评论 0原文

我有一个并不那么复杂的函数,但由于某种原因,每次我调用它时,它都会冻结屏幕大约四分之一秒。该功能基于计时器,每秒部署一次,因此可能会让用户感到非常烦人。

该函数基本上获取屏幕的屏幕截图,通过比较 10 个像素来检查它是否正在寻找的屏幕,如果是屏幕则返回 true,否则返回 false:

ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1); //Capture screen isn't the problem. It gave me no freezes in an endless loop.

这就是类:

class ClassName
{
    public static Pixel[] Pxarr1 = new[]
    {
        new Pixel(Color.FromArgb(255, 204, 204, 170), new Point(15, 145)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 460)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 545)),
        new Pixel(Color.FromArgb(255, 204, 187, 170), new Point(15, 150)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(22, 190)),
        new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(25, 540)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(22, 61)),
        new Pixel(Color.FromArgb(255, 221, 204, 170), new Point(23, 563)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(23, 47)),
        new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(23, 463)),

    };

    public static Pixel[] Pxarr2 = new[]
    {
        new Pixel(Color.FromArgb(255, 221, 255, 119), new Point(80, 120)),
        new Pixel(Color.FromArgb(255, 51, 119, 221), new Point(180, 525)),
        new Pixel(Color.FromArgb(255, 204, 170, 85), new Point(630, 455)),
        new Pixel(Color.FromArgb(255, 85, 153, 17), new Point(707, 177)),
        new Pixel(Color.FromArgb(255, 255, 153, 34), new Point(520, 440)),
        new Pixel(Color.FromArgb(255, 255, 238, 51), new Point(150, 325)),
        new Pixel(Color.FromArgb(255, 0, 85, 255), new Point(455, 70)),
        new Pixel(Color.FromArgb(255, 255, 221, 51), new Point(685, 285)),
        new Pixel(Color.FromArgb(255, 17, 17, 17), new Point(547, 369)),
        new Pixel(Color.FromArgb(255, 170, 170, 136), new Point(500, 545)),

    };

    public static Pixel[] Pxarr3 = new[]
    {
        new Pixel(Color.FromArgb(255, 238, 238, 238), new Point(353, 223)),
        new Pixel(Color.FromArgb(255, 28, 33, 49), new Point(428, 198)),
        new Pixel(Color.FromArgb(255, 85, 85, 85), new Point(462, 314)),
        new Pixel(Color.FromArgb(255, 221, 221, 238), new Point(450, 450)),
        new Pixel(Color.FromArgb(255, 102, 102, 102), new Point(384, 349)),
        new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(406, 248)),
        new Pixel(Color.FromArgb(255, 221, 221, 221), new Point(464, 453)),
        new Pixel(Color.FromArgb(255, 255, 204, 17), new Point(413, 198)),
        new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(343, 447)),
        new Pixel(Color.FromArgb(255, 255, 255, 255), new Point(403, 457)),

    };

    public static Pixel[] Pxarr4 = new[]
    {
        new Pixel(Color.FromArgb(255, 204, 170, 136), new Point(120, 227)),
        new Pixel(Color.FromArgb(255, 221, 187, 153), new Point(502, 170)),
        new Pixel(Color.FromArgb(255, 119, 85, 34), new Point(692, 243)),
        new Pixel(Color.FromArgb(255, 238, 221, 187), new Point(211, 169)),
        new Pixel(Color.FromArgb(255, 187, 170, 136), new Point(272, 238)),
        new Pixel(Color.FromArgb(255, 170, 153, 119), new Point(696, 64)),
        new Pixel(Color.FromArgb(255, 136, 85, 17), new Point(306, 242)),
        new Pixel(Color.FromArgb(255, 187, 170, 119), new Point(115, 236)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(310, 183)),
        new Pixel(Color.FromArgb(255, 153, 102, 34), new Point(647, 245)),

    };

    public static Pixel[] Pxarr5 = new[]
    {
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 350)),

        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 350)),

        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 350))
    };

    public static bool CheckScreen(Bitmap img, Pixel[] samples)
    {
        int verifiedpixels = 0;
        for (int i = 0; i < samples.Length; i++)
        {
            Color c = img.GetPixel(samples[i].Location.X, samples[i].Location.Y);
            if (c == samples[i].Color)
                    verifiedpixels++;
        }
        return verifiedpixels >= (samples.Length * 0.6);
    }

}

注意:100 个循环 for 语句的速度是CaptureScreen() 与使用文件中的图像相比。 (4 秒与 2 秒)

但是,我仍然不明白为什么这个函数会导致冻结,即使使用文件中的图像也是如此。

I have a function which isn't so complicated, but for some reason, each time I call it, it freezes the screen for like quarter of a second. This function is on a timer and is deployed every second so it can get pretty annoying to the user.

The function basically gets a screen shot of the screen, checks if the screen that it's looking for by comparing 10 pixels and returns true if that's the screen or false otherwise :

ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1); //Capture screen isn't the problem. It gave me no freezes in an endless loop.

Thats the class :

class ClassName
{
    public static Pixel[] Pxarr1 = new[]
    {
        new Pixel(Color.FromArgb(255, 204, 204, 170), new Point(15, 145)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 460)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(20, 545)),
        new Pixel(Color.FromArgb(255, 204, 187, 170), new Point(15, 150)),
        new Pixel(Color.FromArgb(255, 221, 204, 187), new Point(22, 190)),
        new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(25, 540)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(22, 61)),
        new Pixel(Color.FromArgb(255, 221, 204, 170), new Point(23, 563)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(23, 47)),
        new Pixel(Color.FromArgb(255, 204, 204, 187), new Point(23, 463)),

    };

    public static Pixel[] Pxarr2 = new[]
    {
        new Pixel(Color.FromArgb(255, 221, 255, 119), new Point(80, 120)),
        new Pixel(Color.FromArgb(255, 51, 119, 221), new Point(180, 525)),
        new Pixel(Color.FromArgb(255, 204, 170, 85), new Point(630, 455)),
        new Pixel(Color.FromArgb(255, 85, 153, 17), new Point(707, 177)),
        new Pixel(Color.FromArgb(255, 255, 153, 34), new Point(520, 440)),
        new Pixel(Color.FromArgb(255, 255, 238, 51), new Point(150, 325)),
        new Pixel(Color.FromArgb(255, 0, 85, 255), new Point(455, 70)),
        new Pixel(Color.FromArgb(255, 255, 221, 51), new Point(685, 285)),
        new Pixel(Color.FromArgb(255, 17, 17, 17), new Point(547, 369)),
        new Pixel(Color.FromArgb(255, 170, 170, 136), new Point(500, 545)),

    };

    public static Pixel[] Pxarr3 = new[]
    {
        new Pixel(Color.FromArgb(255, 238, 238, 238), new Point(353, 223)),
        new Pixel(Color.FromArgb(255, 28, 33, 49), new Point(428, 198)),
        new Pixel(Color.FromArgb(255, 85, 85, 85), new Point(462, 314)),
        new Pixel(Color.FromArgb(255, 221, 221, 238), new Point(450, 450)),
        new Pixel(Color.FromArgb(255, 102, 102, 102), new Point(384, 349)),
        new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(406, 248)),
        new Pixel(Color.FromArgb(255, 221, 221, 221), new Point(464, 453)),
        new Pixel(Color.FromArgb(255, 255, 204, 17), new Point(413, 198)),
        new Pixel(Color.FromArgb(255, 204, 204, 204), new Point(343, 447)),
        new Pixel(Color.FromArgb(255, 255, 255, 255), new Point(403, 457)),

    };

    public static Pixel[] Pxarr4 = new[]
    {
        new Pixel(Color.FromArgb(255, 204, 170, 136), new Point(120, 227)),
        new Pixel(Color.FromArgb(255, 221, 187, 153), new Point(502, 170)),
        new Pixel(Color.FromArgb(255, 119, 85, 34), new Point(692, 243)),
        new Pixel(Color.FromArgb(255, 238, 221, 187), new Point(211, 169)),
        new Pixel(Color.FromArgb(255, 187, 170, 136), new Point(272, 238)),
        new Pixel(Color.FromArgb(255, 170, 153, 119), new Point(696, 64)),
        new Pixel(Color.FromArgb(255, 136, 85, 17), new Point(306, 242)),
        new Pixel(Color.FromArgb(255, 187, 170, 119), new Point(115, 236)),
        new Pixel(Color.FromArgb(255, 204, 187, 153), new Point(310, 183)),
        new Pixel(Color.FromArgb(255, 153, 102, 34), new Point(647, 245)),

    };

    public static Pixel[] Pxarr5 = new[]
    {
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(376, 350)),

        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(506, 350)),

        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 150)),
        new Pixel(Color.FromArgb(255, 255, 238, 221), new Point(246, 350))
    };

    public static bool CheckScreen(Bitmap img, Pixel[] samples)
    {
        int verifiedpixels = 0;
        for (int i = 0; i < samples.Length; i++)
        {
            Color c = img.GetPixel(samples[i].Location.X, samples[i].Location.Y);
            if (c == samples[i].Color)
                    verifiedpixels++;
        }
        return verifiedpixels >= (samples.Length * 0.6);
    }

}

Note : A 100 loop for statement was twice as slow with CaptureScreen() than with an image from a file.
(4 seconds vs 2 seconds)

But still, I don't understand why this function casuses any freezing, even with an image from a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜吻♂芭芘 2024-12-02 22:19:36

由于您使用的是计时器,因此它不应冻结 UI,除非您使用 System.Windows.Forms.Timer 它将在 UI 线程上执行回调。而是使用 System.Timers.TimerSystem.Threading.Timer
System.Timers.Timer 示例:

private System.Timers.Timer _checkPixelsTimer = new System.Timers.Timer();

public Form1()
{
    InitializeComponents();

    _checkPixelsTimer.Intervla = 1000;//one second
    _checkPixelsTimer.AutoReset = true;
    _checkPixelsTimer.Elapced += OnCheckPixelsTimerElapced;
    _checkPixelsTimer.Start();
}

private void OnCheckPixelsTimerElapced(object sender, System.Timer.ElapcedEventArgs e)
{
    if (ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1))
    {
        MethodInvoker method = new MethodInvoker(() => { /*code that relays on the UI thread */ });
        if (this.InvokeRequired)
        {
            this.Invoke(method); 
        }
        else
        {
            method();
        }
    }
}

编辑:更新代码以预览如何在 UI 线程调用函数。

Since you are using a timer, it should not freeze the UI, unless you are using System.Windows.Forms.Timer which will execute its call back on the UI thread. Instead use System.Timers.Timer or System.Threading.Timer.
Example for System.Timers.Timer:

private System.Timers.Timer _checkPixelsTimer = new System.Timers.Timer();

public Form1()
{
    InitializeComponents();

    _checkPixelsTimer.Intervla = 1000;//one second
    _checkPixelsTimer.AutoReset = true;
    _checkPixelsTimer.Elapced += OnCheckPixelsTimerElapced;
    _checkPixelsTimer.Start();
}

private void OnCheckPixelsTimerElapced(object sender, System.Timer.ElapcedEventArgs e)
{
    if (ClassName.CheckScreen(CaptureScreen(),ClassName.Pxarr1))
    {
        MethodInvoker method = new MethodInvoker(() => { /*code that relays on the UI thread */ });
        if (this.InvokeRequired)
        {
            this.Invoke(method); 
        }
        else
        {
            method();
        }
    }
}

Edit: update the code to preview how to call a function at the UI thread.

被你宠の有点坏 2024-12-02 22:19:36

GetPixel() 和 SetPixel() 是出了名的慢。

使用替代方法,例如 LockBits()/UnlockBits() 技术。请参阅在 C# 中快速使用位图

GetPixel() and SetPixel() are notoriously slow.

Use an alternative method such as the LockBits()/UnlockBits() technique. See Fast work with Bitmaps in C#

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文