简体   繁体   English

检测 Winforms 中的空闲用户

[英]Detecting idle users in Winforms

I'd like to pause my program if a user is inactive for 5 minutes.如果用户闲置 5 分钟,我想暂停我的程序。 By inactive I mean hasn't pressed their mouse or their keyboard during that time (including outside the program too.)?不活动我的意思是在那段时间里没有按下他们的鼠标或键盘(也包括在程序之外。)? Any starting points?有什么出发点吗?

Within a timer you could p/invoke GetLastInputInfo() which will return the number ms since input was detected from the user, across all processes in the current session.在计时器内,您可以 p/调用GetLastInputInfo() ,它将返回自检测到用户输入以来的毫秒数,跨越当前 session 中的所有进程。

This question is very similar to an old question:这个问题与一个老问题非常相似:

.NET equivalent for GetLastInputInfo? .NET 等效于 GetLastInputInfo?

Which referenced a good article describing some different options in C#:其中引用了一篇描述 C# 中一些不同选项的好文章:

http://www.codeproject.com/KB/cs/uim.aspx http://www.codeproject.com/KB/cs/uim.aspx

If you don't mind using P/Invoke and being limited to running on windows systems, then using P/Invoke to call GetLastInputInfo() is probably the simplest method of accomplishing what you want.如果您不介意使用 P/Invoke 并且仅限于在 windows 系统上运行,那么使用 P/Invoke 调用 GetLastInputInfo() 可能是完成您想要的最简单的方法。

you could override the WndProc and look for the WM_IDLE message您可以覆盖 WndProc 并查找 WM_IDLE 消息

I've used the GMA.UserActivityMonitor library a while ago to achieve this.不久前我使用了GMA.UserActivityMonitor库来实现这一点。

Looks like I found solution for Your problem, look at:看起来我找到了解决您的问题的方法,请查看:

How to detect a Winforms app has been idle for certain amount of time and user34660 answer, 如何检测 Winforms 应用程序已空闲一段时间user34660回答,

For your app, must change:对于您的应用,必须更改:

const int MinuteMicroseconds = 60000;

to

const int MinuteMicroseconds = 300000;

It's just time, but in ms.这只是时间,但以毫秒为单位。

Works nice for my Windows Forms application.适用于我的 Windows Forms 应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM