1

我想在我的 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;
        }

    }
4

1 回答 1

0

试着带你debug.writeline出去——它慢,因为你可能要处理很多事件,这很容易成为问题。

如果做不到这一点,您是否尝试过使用分析器查看正在消耗资源的内容?

于 2012-11-12T16:01:56.820 回答