0

我在 WPF 中完成了一个应用程序,使用 MvvM/Prism/Unity 和 Remote 作为数据源。

我需要一个基本的东西,在win表单上真的很简单,几分钟后检查应用程序是否空闲,如果空闲,锁定应用程序并显示登录屏幕。

在谷歌上进行一些搜索后,我发现一个使用 DllImport 的解决方案和另一个使用纯 Wpf 方法的解决方案。

我不知道,在我实现了 Wpf 方式之后(请检查下面的代码),它仅在我登录应用程序后才有效,如果我打开并单击一个简单的 texbox 或点击搜索,idle 方法不会被触发,看起来背景中挂着什么东西,使 Wpf 空闲例程认为它在做某事而不是在做某事。

如何检查内存中与 may app 相关的所有服务/方法/等?callstack 对我来说并没有显示太多。我很害怕,或者我没有以正确的方式调用远程服务,或者我在道具 PropChanged 事件/observablecollections/等上实现了错误...

有没有更好的方法使用 100% Wpf 结构来做到这一点?

private void CheckIdleTime()
{

    handler = delegate
    {
        DispatcherTimer timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromSeconds(5);
        timer.Tick += delegate
        {
            if (timer != null)
            {
                timer.Stop();
                timer = null;
                System.Windows.Interop.ComponentDispatcher.ThreadIdle -= handler;

                Console.WriteLine("IDLE! Lets logoff!");

                this.LockApplication();

                Console.WriteLine("logoff fired");

                System.Windows.Interop.ComponentDispatcher.ThreadIdle += handler;
            }

        };

        timer.Start();


        Dispatcher.CurrentDispatcher.Hooks.OperationPosted += delegate
        {
            if (timer != null)
            {
                timer.Stop();
                timer = null;
            }
        };
    };

    ComponentDispatcher.ThreadIdle += handler;
}
4

1 回答 1

1

将有默认窗口事件找到空闲时间......我认为如果我们对 wpf 或任何其他应用程序使用相同的事件将是明智的......

以下链接将帮助您实现它..

Application.Idle 事件未在 WPF 应用程序中触发

于 2012-01-13T12:47:09.260 回答