WPF自动注销挂钩减慢了GUI的速度

时间:2012-11-12 15:57:54

标签: wpf hook

我想在我的WPF应用程序中使用自动注销功能并使用钩子实现它。但是,只要鼠标悬停在应用程序上,性能就会冻结,降级并变得难以忍受地无响应。一旦鼠标关闭,窗口性能就会恢复正常。如果我关闭自动注销,性能总是很好,所以它肯定会导致它。知道怎么做不同以避免这种情况吗?

private void InitializeAutoLogoffFeature()
        {
            //var windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            //if (windowSpecificOSMessageListener != null)
            //    windowSpecificOSMessageListener.AddHook(CallBackMethod);

            //AutoLogOffHelper.LogOffTime = _viewModel.logOffTime;
            //AutoLogOffHelper.MakeAutoLogOffEvent += AutoLogOffHelper_MakeAutoLogOffEvent;
            //AutoLogOffHelper.StartAutoLogoffOption();
            AutoLogOffHelper
        }

private static IntPtr CallBackMethod(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            try
            {
                //  Listening OS message to test whether it is a user activity
                if ((msg >= 0x0200 && msg <= 0x020A) || (msg <= 0x0106 && msg >= 0x00A0) || msg == 0x0021)
                {
                    AutoLogOffHelper.ResetLogoffTimer();
                }
                else
                {
                    // For debugging purpose
                    // If this auto logoff does not work for some user activity, you can detect the integer code of that activity  using the following line.
                    //Then All you need to do is adding this integer code to the above if condition.
                    Debug.WriteLine(msg.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageHelper.LogError(ex);
            }

            return IntPtr.Zero;
        }



class AutoLogOffHelper
    {
        static System.Windows.Forms.Timer _timer;
        public static int LogOffTime { get; set; }

        public delegate void MakeAutoLogOff();
        static public event MakeAutoLogOff MakeAutoLogOffEvent;

        static public void StartAutoLogoffOption()
        {
            System.Windows.Interop.ComponentDispatcher.ThreadIdle += DispatcherQueueEmptyHandler;
        }

        static void _timer_Tick(object sender, EventArgs e)
        {
            if (_timer == null) return;

            System.Windows.Interop.ComponentDispatcher.ThreadIdle -= DispatcherQueueEmptyHandler;
            _timer.Stop();
            _timer = null;

            if (MakeAutoLogOffEvent != null)
            {
                MakeAutoLogOffEvent();
            }
        }

        static void DispatcherQueueEmptyHandler(object sender, EventArgs e)
        {
            if (_timer == null)
            {
                _timer = new System.Windows.Forms.Timer
                             {
                                 Interval = LogOffTime * 60 * 1000
                             };

                _timer.Tick += _timer_Tick;
                _timer.Enabled = true;
            }
            else if (_timer.Enabled == false)
            {
                _timer.Enabled = true;
            }
        }

        static public void ResetLogoffTimer()
        {
            if (_timer == null) return;
            _timer.Enabled = false;
            _timer.Enabled = true;
        }

    }

1 个答案:

答案 0 :(得分:0)

尝试将debug.writeline取出 - 它的非常很慢,因为您可能正在处理很多事件,所以很容易就会出现问题。

如果不这样做,您是否尝试使用分析器来查看占用资源的内容?