wpf应用程序中的自动下线服务

本文关键字:服务 应用程序 wpf | 更新日期: 2023-09-27 18:04:15

我试图在wpf解决方案中创建一个自动注销服务,该服务列出了键盘和鼠标的使用情况。此刻我使用这个代码(见下文)。但它对除主窗口之外的任何窗口都不起作用。有没有其他的方法来处理这个没有附加到主窗口?

 public AutoLogOffService(
            System.Windows.Window applicationWindow,
            IUserProfileManager userProfileManager,
            IDefaultUserDataProvider defaultUserDataProvider,
            IEventAggregator eventAggregator,
            int inactivityInterval)
        {
            _userProfileManager = userProfileManager;
            _defaultUser = defaultUserDataProvider.GetDefaultUser();
            _lastActivity = DateTime.Now;
            var timer = new Timer(1000);
            timer.Elapsed += (sender, args) =>
            {
                //report
                if (DisableAutoLogout)
                {
                    eventAggregator.Publish<ILogOffServiceTimeRemaining>(x =>
                    {
                        x.Percent = 100;
                        x.Seconds = inactivityInterval * 60;
                        x.AutoLogOffDisabled = true;
                    });
                }
                else
                {
                    var remainingSeconds = Convert.ToInt32((_lastActivity.AddMinutes(inactivityInterval) - DateTime.Now).TotalSeconds);
                    remainingSeconds = remainingSeconds < 0 ? 0 : remainingSeconds;
                    var remainingPercent = (int)((double)remainingSeconds / (inactivityInterval * 60) * 100);
                    eventAggregator.Publish<ILogOffServiceTimeRemaining>(x =>
                    {
                        x.Percent = remainingPercent;
                        x.Seconds = remainingSeconds;
                    });
                }
                if (DisableAutoLogout == false && _userProfileManager.CurrentUser != _defaultUser
                    && _lastActivity < DateTime.Now - TimeSpan.FromMinutes(inactivityInterval))
                {
                    DispatcherHelper.SafeInvoke(() => _userProfileManager.CurrentUser = _defaultUser);
                }
            };
            timer.Start();
            var windowSpecificOSMessageListener = HwndSource.FromHwnd(new WindowInteropHelper(applicationWindow).Handle);
            if (windowSpecificOSMessageListener != null)
            {
                windowSpecificOSMessageListener.AddHook((IntPtr hwnd, int msg, IntPtr param, IntPtr lParam, ref bool handled) =>
                {
                    //  Listening OS message to test whether it is a user activity
                    if ((msg >= 0x0200 && msg <= 0x020A) || (msg <= 0x0106 && msg >= 0x00A0) || msg == 0x0021)
                    {
                        //Debug.WriteLine("Message {0:X}", msg);
                        _lastActivity = DateTime.Now;
                    }
                    return IntPtr.Zero;
                });
            }
        }

wpf应用程序中的自动下线服务

您可以使用Application.Current.Windows对象访问WPF应用程序中的每个Window:

foreach (Window window in Application.Current.Windows)
{
    AutoLogOffService autoLogOffService = new AutoLogOffService(window);
}

您也可以直接选择特定类型的自定义Window s

foreach (CustomWindow window in Application.Current.Windows.OfType<CustomWindow>())
{
    AutoLogOffService autoLogOffService = new AutoLogOffService(window);
}