Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My project is in here. This application shows status of capslock,numlock and scrolllock on the screen. Sometimes while it is running, my computer freezes.

//Program.cs
using System;
using System.Windows.Forms;

namespace Indicator
{
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;

    using Timer = System.Windows.Forms.Timer;

    static class Program
    {
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        private static Popup form2;
        private static Timer timer;

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true,
    CallingConvention = CallingConvention.Winapi)]
        public static extern short GetKeyState(int keyCode);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);

        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            _hookID = SetHook(_proc);
            form2 = new Popup();
            timer = new Timer { Interval = 2000 };
            timer.Tick += timer1_Tick;
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }

        private static void timer1_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            form2.Opacity = 0.9;
            for (double i = 0.9; 0 <= i; i -= 0.02)
            {
                Thread.Sleep(10);
                form2.Opacity = i;
            }

            form2.Hide();
            form2.Opacity = 0.9;
        }


        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (var curProcess = Process.GetCurrentProcess())
            using (var curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                if ((Keys)vkCode == Keys.Capital)
                {
                    var CapsLock = (((ushort)GetKeyState(0x14)) & 0xffff) != 0;
                    timer.Stop();
                    timer.Start();

                    form2.label1.Text = CapsLock ? "CapsLook Kapalı" : "CapsLook Açık";
                    form2.Show();
                }
                else if ((Keys)vkCode == Keys.NumLock)
                {
                    var NumLock = (((ushort)GetKeyState(0x90)) & 0xffff) != 0;
                    timer.Stop();
                    timer.Start();

                    form2.label1.Text = NumLock ? "NumLock Kapalı" : "NumLock Açık";
                    form2.Show();
                }
                else if ((Keys)vkCode == Keys.Scroll)
                {
                    var ScrollLock = (((ushort)GetKeyState(0x91)) & 0xffff) != 0;
                    timer.Stop();
                    timer.Start();

                    form2.label1.Text = ScrollLock ? "ScrollLock Kapalı" : "ScrollLock Açık";
                    form2.Show();
                }
            }

            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
    }
}

//Popup.cs
namespace Indicator
{
    using System.Windows.Forms;

    /// <summary>
    /// The form 2.
    /// </summary>
    public partial class Popup : Form
    {
        public Popup()
        {
            InitializeComponent();
            Top = 50;
            Left = 50;
        }
    }
}
share|improve this question
The whole computer freezes? Can you Ctrl-Alt-Del? – Bobson Dec 20 '12 at 22:37
No , only keyboard freezes briefly while I am typing. there is no need ctrl+alt+del. – sinanakyazici Dec 20 '12 at 22:48
You mean you're asking us to find the error in your code? In that case, your question is off topic here, per the FAQ. – svick Dec 21 '12 at 17:09
@svick I did not say anything like that. There is no error in the code. Only keyboard freezes while the form2 closing because of I used thread.sleep. The goal is ask to question, how to code was to make the better performance and a cleaner. I thought that you are able to grasp them without explanation. I do not thing I have do not understand why the question is closed. – sinanakyazici Dec 23 '12 at 6:11
“The keyboard freezes, how do I fix that?” is not a performance problem, it's code that's not working properly. Which is why your question is off topic here. – svick Dec 23 '12 at 9:44
show 2 more comments

closed as off topic by svick, Jeff Vanzella, Brian Reichle, Paul, Trevor Pilley Dec 23 '12 at 0:02

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.