[英]Getting inactivity/idle time in a WPF application
I was looking for the best approach to find out the if my users are idle in my WPF application. 我正在寻找最好的方法来找出我的用户是否在我的WPF应用程序中空闲。 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.
目前,我从操作系统中获取这个空闲时间,如果它们最小化应用程序,并在Internet上搜索,则操作系统中有一个进程,因此操作系统不会将此视为非活动时间,即使它们没有进行应用程序内的任何内容 However, I would like to find out if they have not clicked or do anything inside my application.
但是,我想知道他们是否没有点击或在我的申请中做任何事情。
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
}
}
public MainWindow()
{
InitializeComponent();
ComponentDispatcher.ThreadIdle += new System.EventHandler(ComponentDispatcher_ThreadIdle);
}
void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
{
//do your idle stuff here
}
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是您要查找的事件。
For such scenarios, we need a Timer
which fires back some event
after a timeinterval
. 对于这种情况,我们需要一个
Timer
,它会在一个时间timeinterval
后激活一些event
。
And most importantly, we need a callback / eventhandler
which gets called everytime any activity of any kind happens in our application, so that we know that our application is running
. 最重要的是,我们需要一个
callback / eventhandler
程序,每次在我们的应用程序中发生任何类型的任何活动时callback / eventhandler
它,以便我们知道我们的应用程序正在running
。
Point 1 can be handled using DispatcherTimer
. 可以使用
DispatcherTimer
处理点1。
Point 2 can be handled using public event CanExecuteRoutedEventHandler CanExecute;
可以使用
public event CanExecuteRoutedEventHandler CanExecute;
来处理点2 public event CanExecuteRoutedEventHandler CanExecute;
. 。
Putting it altogether : 完全放在一起:
MainWindow.xaml MainWindow.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 MainWindow.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. 我们可以轻松扩展这种方法以满足我们的需求。
How about subscribing to SENS events? 订阅SENS活动怎么样?
Here's another link . 这是另一个链接 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.