Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.

C#
[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;
            }

Any one help me Thnaks Advance!...
Posted
Updated 14-Oct-15 20:18pm
v2

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
 
Share this answer
 
Comments
Member 11889799 15-Oct-15 3:02am    
Hi Amy,
I am see your given link but its working for normal mouse, I am using 3d mouse the coding not consider the 3d mouse if i am working with 3d mouse that coding consider the idle time why it is not consider.(Another Example windows media player is running my system but the coding is show is idle time.)
Sergey Alexandrovich Kryukov 15-Oct-15 9:30am    
You see, I must note that your behavior as the inquirer is quite unreasonable. You mentioned about the use of your non-standard mouse only now, when _Any already wasted time on answering you. You should have mentioned it in first place. You did not show your real code. I say so because you could not possibly use your third [DLLImport] line where it is now, before the assignment operator. How can you expect help when you show misleading information? And you never explained what time you really want to get.

See also Solution 2.

—SA
_Amy 15-Oct-15 20:09pm    
Thank you SA. :)
Sergey Alexandrovich Kryukov 15-Oct-15 23:54pm    
You are very welcome.
—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
 
Share this answer
 
Comments
_Amy 15-Oct-15 20:11pm    
+5! You did my job. :)
Sergey Alexandrovich Kryukov 15-Oct-15 23:54pm    
Thank you, Amy.
—SA
Member 12859293 19-Nov-16 23:30pm    
getlastinputinfo cannot in windows server? How to solute this proplem

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900