简体   繁体   中英

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. Once I've removed those my form is not monitoring Caps Lock state, any suggestions?

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.

Honestly, keeping the CapsLock status constantly monitored, even when the application is idle, looks like an overkill to me. 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. Even if the code is based on WPF framework, you have an excellent example of what I'm talking about here .

In order to perform what you are looking for, you need to set up a very complex system based on 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. The link also contains a demo code written in C# that you can deploy and try.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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