如何获得系统空闲时间? [英] how to get a system Idle Time?

查看:158
本文介绍了如何获得系统空闲时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我正在使用VS2010和c#语言我需要获得系统空闲时间我正在使用以下代码来获取系统空闲时间,当用户不与鼠标交互时它工作正常和键盘,但有时用户处理一些其他应用程序,而不需要鼠标和键盘,所以时间也考虑系统空闲时间但过程运行如何我解决这种情况。



Dear All,
I am using VS2010 and c# language i need to get the system idle time i am using following code for get the system idle time its working fine when user not interact with the mouse and keyboard but some times user processed some other application without need of the mouse and key board so that time also its consider the system idle time but process is running how to i solve the situation.

[DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_HIDE = 0;
        const int SW_SHOW = 5;

        [DllImport("user32.dll")]
//Get Idle Time
        tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
            
            LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
            LastInput.dwTime = 0;

            if (GetLastInputInfo(ref LastInput))
            {

                tmpcnt = IdleTime;

                IdleTime = System.Environment.TickCount - LastInput.dwTime;
            }



任何人帮我Thnaks Advance!...


Any one help me Thnaks Advance!...

推荐答案

你可以使用 user32.dll LASTINPUTINFO [ ^ ]计算系统的理想时间。



这是类似的主题:

如何计算系统空闲超时让机器进入睡眠状态? [ ^ ]

使用C#获取用户空闲时间 [ ^ ] br />
系统空闲时间检测在C#.Net [ ^ ]





- 艾米
You can use user32.dll and LASTINPUTINFO[^] to calculate the system ideal time.

Here is the similar threads:
How to calculate system idle time out and put the machine to sleep?[^]
Getting the user idle time with C#[^]
System Idle Time Detection in C#.Net[^]


--Amy


首先,请看我对解决方案1的评论。



你的要求很不清楚,但在某种程度上,你的其他一些应用程序,不需要鼠标和键盘,特别是媒体播放器正在运行我的系统,但编码显示是空闲时间告诉尾部。



粗略地说,听起来好像你认为监视器是输入设备。 不是。你所谓的空闲时间并不完全是Windows中通常所说的空闲,但我们试着接受你的术语。通过使用 GetLastInpitInfo ,您将找到用户不活动的时间。显示视频不是用户活动。操作系统不能将此时间计算为某些活动的时间。你对闲置的定义似乎是这样的:计算机没有做任何有用的时间。



所以,如果我理解你的话,我的回答是:你无法确定以这种方式定义的空闲时间,原因很简单:操作系统不知道什么活动有用,什么不是。你没有提出任何合理的闲置标准。操作系统可以暂时关闭显示器,但显然这不是你的意思。你想告诉监视器显示一些电影和什么都不做的区别。但这还不够,因为如果监视器中没有任何变化,它仍然可以在活动时间内发生:某些视频可以显示但是Z顺序顶部的某些其他窗口不允许看到它,或者某些进程确实一些计算并没有在屏幕上显示更改,稍后会显示它们,依此类推。您需要了解:对于操作系统,没有区别。没有技术方法来定义差异,所以你的请求甚至没有意义。



对于你的3D鼠标,它听起来很可疑。即使是3D鼠标,如果它像真正的系统鼠标一样,也应该触发低级输入事件,应该在 GetLastInpitInfo 中计算。如果没有发生,那就是你的问题;您没有在此设备上分享任何信息,因此请自行处理。但坦率地说,我不相信你的结论是正确的。



-SA
First of all, please see my comment to Solution 1.

Your request is quite unclear, but, to certain extent, your "some other application without need of the mouse and key board" and especially "media player is running my system but the coding is show is idle time" tell the tail.

It sounds, roughly speaking, as if you considered monitor as an input device. It is not. What you call "idle time" is not exactly what is usually called "idle" in Windows, but let's try to accept your terminology. By using GetLastInpitInfo, you are finding the time of user inactivity. Showing video is not user activity. The OS cannot count this time as time of some activity. Your definition of "idle" seems to be something like that: "the time when the computer does not do anything useful".

So, if I understand your correctly, my answer is: you cannot determine the idle time "defined" this way, by one simple reason: the OS does not "know" what activity is useful and what's not. You did not suggest any reasonable criteria of "idleness". The monitor can be switched off temporarily by OS, but apparently this is not what you mean. You want to tell the difference between monitor showing some motion picture and doing nothing. But this is not enough, because if nothing is changed in monitor, it still can happen during the active time: some video can be shown but some other window on the top in Z-order does not allow to see it, or some process does some calculations and not showing changes on screen and will show them a bit later, and so on. You need to understand: for the OS, there is no difference. There is no a technical way to define the difference, so your request does not even make sense.

As to your 3D mouse, it sounds suspicious. Even a 3D mouse, if it behaves as a real system mouse, should trigger low-level input events which should be accounted in GetLastInpitInfo. If it does not happen, it's your problem; you did not share any information on this device, so deal with it yourself. But frankly, I don't believe that your conclusion is correct.

—SA


这篇关于如何获得系统空闲时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆