C# 在 WPF 应用程序中获取不活动/空闲时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1111615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:30:37  来源:igfitidea点击:

Getting inactivity/idle time in a WPF application

c#wpf

提问by paradisontheitroad

I was looking for the best approach to find out the if my users are idle in my WPF application. Currently, I take this idle time from operating system, and if they minimize the application, and go and search in Internet, there is a process in the operating system, so Operation system does not consider this as inactivity time even though they are not doing anything inside the application. However, I would like to find out if they have not clicked or do anything inside my application.

我一直在寻找最佳方法来确定我的用户是否在我的 WPF 应用程序中空闲。目前,我把这个空闲时间从操作系统中取出来,如果他们最小化应用程序,然后在互联网上搜索,操作系统中有一个进程,所以即使他们没有这样做,操作系统也不认为这是不活动时间应用程序中的任何内容。但是,我想知道他们是否没有在我的应用程序中单击或执行任何操作。

This is how I can that idle time right now.

这就是我现在空闲时间的方式。

myApplication.MainMethod()
    {
        System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        myTimer .Interval = 1000;
        myTimer .Tick += new EventHandler(Timer_Tick);
        myTimer .Start();
    }

    void Timer_Tick(object sender, EventArgs e)
    {
        int idleTime= (int)Win32.GetIdleTime();
        if (idleTime<certainNumber)
        {
         //do this
         }
    }

回答by Sergey Aldoukhov

How about subscribing to SENSevents?

订阅SENS事件怎么样?

Here's another link.

这是另一个链接

回答by xt1

See this answer: Application.Idle event not firing in WPF application

请参阅此答案: Application.Idle 事件未在 WPF 应用程序中触发

ComponentDispatcher.ThreadIdle is the event you are looking for.

ComponentDispatcher.ThreadIdle 是您正在寻找的事件。

回答by Andreas

public MainWindow()
    {
        InitializeComponent();
        ComponentDispatcher.ThreadIdle += new System.EventHandler(ComponentDispatcher_ThreadIdle);
    }

void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
    {
        //do your idle stuff here
    }

回答by AnjumSKhan

  1. For such scenarios, we need a Timerwhich fires back some eventafter a timeinterval.

  2. And most importantly, we need a callback / eventhandlerwhich gets called everytime any activity of any kind happens in our application, so that we know that our application is running.

  1. 对于这种情况,我们需要 a在 a 之后Timer回火。eventtimeinterval

  2. 最重要的是,我们需要callback / eventhandler每次在我们的应用程序中发生任何类型的活动时都会调用它,以便我们知道我们的应用程序是running.

Point 1 can be handled using DispatcherTimer.

可以使用 处理第 1 点DispatcherTimer

Point 2 can be handled using public event CanExecuteRoutedEventHandler CanExecute;.

可以使用 处理第 2 点public event CanExecuteRoutedEventHandler CanExecute;

Putting it altogether :

总而言之:

MainWindow.xaml

主窗口.xaml

xmlns:cmd="clr-namespace:System.Windows.Input; assembly=Presentationcore"

xmlns:cmd="clr-namespace:System.Windows.Input; assembly=Presentationcore"

    <!-- a do nothing button -->
    <Button  Command="{x:Static cmd:NavigationCommands.BrowseBack}" Visibility="Collapsed">
        <Button.CommandBindings>
    <!-- we can use any pre-defined / user-defined command -->
            <CommandBinding Command="{x:Static cmd:NavigationCommands.BrowseBack}" CanExecute="CommandBinding_CanExecute_1"/>
        </Button.CommandBindings>
    </Button>

MainWindow.xaml.cs

主窗口.xaml.cs

    public partial class MainWindow: Window
    {
        DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.ApplicationIdle);
        bool running = true;

        public MainWindow()
        {
            InitializeComponent();
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        private void CommandBinding_CanExecute_1(object sender, CanExecuteRoutedEventArgs e)
        {
            running = true;
            e.CanExecute = true;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (!running)
                App.Current.Shutdown();

            running = false;
        }
    }

We can easily extend this approach to fit our needs.

我们可以轻松扩展这种方法以满足我们的需求。