C# - Detect Time of Last User Interaction with the Os

C# - Detect time of last user interaction with the OS

GetLastInputInfo. Documented at PInvoke.net.

Windows system idle time calculations and total of time duration idle in c# code?

First, you have two methods, GetIdleTime and GetLastInputTime. They are almost identical except that the former returns the idle time in milliseconds while the later returns it in seconds by idleTime / 1000. You just need the following to get the idle time.

public class SomeClass
{
public static uint GetIdleTime()
{
var info = new LASTINPUTINFO();

info.cbSize = (uint)Marshal.SizeOf(info);

return GetLastInputInfo(ref info)
? (uint)Environment.TickCount - info.dwTime
: 0;
}

[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public uint cbSize;
[MarshalAs(UnmanagedType.U4)]
public uint dwTime;
}

[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}

Second, you've said:

Last 40 minutes not using keyboard and mouse, then returned output below...

From your test results, this is not accurate because the idle time is not incrementing as it should. (you have 125266, 22203, 101329, and 3172 respectively) which means breaking the user-idle state multiple times during that duration by an input device or code. Otherwise, the GetIdleTime method should return 2,400,000 milliseconds (40 minutes).

Third, consider the following code snippets.

// Get and print the idle time in milliseconds...
var it = SomeClass.GetIdleTime();
Console.WriteLine(it);

// Create and print a TimeSpan...
var itTimeSpan = TimeSpan.FromMilliseconds(it)
Console.WriteLine(itTimeSpan.ToString("hh\\:mm\\:ss"));

// Get and print the last input DateTime...
var lastInputTime = DateTime.Now.AddMilliseconds(-it); // Or DateTimeOffset...
Console.WriteLine(lastInputTime);
// Or
Console.WriteLine(lastInputTime.ToString("hh:mm:ss"));

Is there a way to check to see if the user is currently idle?

How about the Win32 LASTINPUTINFO function?

using System.Runtime.InteropServices;

[DllImport("User32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}

What is the best way to track a users usage time in Windows 7 with C#

You could try using GetLastInputInfo for the idle time and a SessionSwitchEventHandler for the workstation lock/unlock.

C# Win Form: Detect last interaction (touch) and display confirmation box

This piece of code did the trick for me:

private void timer1_Tick(object sender, EventArgs e)
{
tagLASTINPUTINFO LastInput = new tagLASTINPUTINFO();
Int32 IdleTime;
LastInput.cbSize = (uint)Marshal.SizeOf(LastInput);
LastInput.dwTime = 0;

if (GetLastInputInfo(ref LastInput))
{
IdleTime = System.Environment.TickCount - LastInput.dwTime;
if (IdleTime > 10000)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
timer1.Stop();
string timeRemaining = IdleTime.ToString();
lbl_TimeRemaining.Text = IdleTime.ToString();
lbl_TimeRemaining.Show();
MessageBox.Show("Do you wish to continue?");
lbl_TimeRemaining.Hide();
}
else
{

}
timer1.Start();
axWindowsMediaPlayer1.Ctlcontrols.play();
}
string title = axWindowsMediaPlayer1.currentMedia.getItemInfo("Title");
}

For the first time you will have to start the timer by calling timer1.start(); from inside the FormLoad() method

Detecting idle users in Winforms

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.



Related Topics



Leave a reply



Submit