奇怪的加载CPU由后台工作者进程

时间:2015-07-29 20:23:14

标签: c# .net winforms

我在后台工作器中有这样的循环,当用户选择一些“编辑”控件时,这会导致我的屏幕键盘恢复。

    private void bwAutoShow_DoWork(object sender, DoWorkEventArgs e)
    {
        IntPtr focused_temp = IntPtr.Zero;
        SetBoolDelegate d_setWindowState = new SetBoolDelegate(SetWindowState);
        int foregroundWindowHandle;
        uint remoteThreadId;
        uint currentThreadId;
        StringBuilder lpClassName = new StringBuilder(100);
        String cn;
        while (autoShow)
        {
            try
            {
                foregroundWindowHandle = GetForegroundWindow();
                remoteThreadId = GetWindowThreadProcessId(foregroundWindowHandle, 0);
                currentThreadId = GetCurrentThreadId();
                //AttachTrheadInput is needed so we can get the handle of a focused window in another app
                AttachThreadInput(remoteThreadId, currentThreadId, true);
                //Get the handle of a focused window
                focused_handle = GetFocus();

                if (focused_handle != focused_temp)
                {                        
                    GetClassName(focused_handle, lpClassName, 100);
                    cn = lpClassName.ToString().ToLower();
                    if (cn.IndexOf("edit") != -1)
                    {
                        Invoke(d_setWindowState, new System.Object[1] { true });
                    }
                    focused_temp = focused_handle;
                }
            }
            catch (System.Exception oException)
            {
                MessageBox.Show("Błąd ogólny procesu auto wyświetlania klawiatury.\n" + oException.ToString(), "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw oException;
            }
            System.Threading.Thread.Sleep(delay);
        }
    }

延迟= 300; 运行应用程序后一切正常,CPU为0%,但当此循环处于活动状态时,CPU负载正在增长。 10分钟后约3-5%,CPU频率为100%。 RAM负载是永久性的。一小时后移动鼠标的光标不是恒定的 - 跳跃:)这个循环出了什么问题?

0 个答案:

没有答案