简体   繁体   English

Windows窗体-高CPU使用率

[英]Windows Form - High CPU usage

newbie here, my form is monitoring Caps Lock status but is using around 50% of CPU, I think this is related to Application.Idle += Application_Idle and Application.Idle -= Application_Idle. 在这里,我的表单正在监视Caps Lock状态,但正在使用大约50%的CPU,我认为这与Application.Idle + = Application_Idle和Application.Idle-= Application_Idle有关。 Once I've removed those my form is not monitoring Caps Lock state, any suggestions? 删除这些表单后,我将不监视Caps Lock状态,有什么建议吗?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CapsLockChecker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Application.Idle += Application_Idle;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        void Application_Idle(object sender, EventArgs e)
        {
            if (Control.IsKeyLocked(Keys.CapsLock))
            {
                label1.Text = "CapsLock is On";
                pictureBox1.ImageLocation = "C:\\Users\\user\\source\\repos\\CapsLockChecker\\CapsLockChecker\\if_Circle_Green_34211.png";
            }
            else
            {
                label1.Text = "CapsLock if Off";
                pictureBox1.ImageLocation = "C:\\Users\\user\\source\\repos\\CapsLockChecker\\CapsLockChecker\\if_Circle_Red_34214.png";
            }
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            Application.Idle -= Application_Idle;
            base.OnFormClosed(e);
        }
    }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.KeyDown += CapsLockMonitor;
        this.KeyPreview = true;
    }

    private void CapsLockMonitor(object sender, KeyEventArgs e)
    {
        if (Control.IsKeyLocked(Keys.CapsLock))
        {
            this.label1.Text = "Caps lock enabled!";
        }
        else
        {
            this.label1.Text = "Caps lock disabled!";
        }
    }
}

This appears to not chew up all my CPU, I subscribe to the KeyDown event with my custom delegate. 这似乎并没有消耗我的所有CPU,我使用自定义委托订阅了KeyDown事件。

Honestly, keeping the CapsLock status constantly monitored, even when the application is idle, looks like an overkill to me. 坦白地说,即使应用程序处于空闲状态,保持CapsLock状态的持续监控,对我来说似乎也太过分了。 I mean, I don't doubt it's a nice feature, but I don't know if it's worth the effort for implementing it. 我的意思是,我毫不怀疑这是一个不错的功能,但是我不知道实现它是否值得付出努力。

If your application has to warn the user whenever he is typing something while his CapsLock is turned on, the best (and simpler) approach would be showing a tooltip or a little warning box somewhere as soon as the user focuses a Control that allows text editing. 如果您的应用程序必须在用户打开CapsLock时在每次输入内容时警告用户,则最好(更简单)的方法是,只要用户将Control放在允许文本编辑的Control ,它就会在某处显示工具提示或小警告框。 Even if the code is based on WPF framework, you have an excellent example of what I'm talking about here . 即使代码是基于WPF框架的,您也可以很好地了解我在这里所说的内容。

In order to perform what you are looking for, you need to set up a very complex system based on Global Keyboard Hook . 为了执行您想要的操作,您需要基于Global Keyboard Hook建立一个非常复杂的系统。 Following this link you can find a very nice and detailed article ( "Processing Global Mouse and Keyboard Hooks in C#" ) describing how to accomplish this task. 通过此链接,您可以找到非常不错且详细的文章( “在C#中处理全局鼠标和键盘挂钩” ),描述如何完成此任务。 The link also contains a demo code written in C# that you can deploy and try. 该链接还包含一个用C#编写的演示代码,您可以对其进行部署和尝试。

A little excerpt: 摘录:

This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all. 此类允许您敲击键盘和鼠标和/或检测其活动,即使应用程序在后台运行或根本没有任何用户界面也是如此。 This class raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need. 此类使用KeyEventArgs和MouseEventArgs引发常见的.NET事件,因此您可以轻松检索所需的任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM