【问题标题】:Auto logoff service in wpf applicationwpf应用程序中的自动注销服务
【发布时间】:2014-05-19 13:38:53
【问题描述】:

我正在尝试在 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;
                });
            }
        }

【问题讨论】:

  • 这是一个糟糕的主意...如果用户有一些未保存的工作,或者他们不想想要保存的更改,或者只是将应用程序留在某个特定的页面或位置,为第二天的工作做好准备了吗?
  • Related(挂钩键盘作为服务)。
  • @Sheridan 。该程序将用于生产环境,我的问题的原因是该程序有很多不受此服务影响的弹出窗口。

标签: c# wpf


【解决方案1】:

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

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

您也可以只选择特定类型的自定义Windows

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 2019-04-01
    • 2011-02-13
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    相关资源
    最近更新 更多