Obtaining idle time via VB.NET.

Happy new year all...

I am trying to get the system idle time via version 4 of the .NET framework using VB.NET.

Public Class idleTime
    Public Structure LASTINPUTINFO
        Public cbSize As UInteger
        Public dwTime As UInteger
    End Structure

    <DllImport("user32.dll")>
    Private Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

    Public Function GetInactiveTime() As String ' Nullable(Of TimeSpan)
        Dim info As LASTINPUTINFO = New LASTINPUTINFO
        info.cbSize = CUInt(Marshal.SizeOf(info))

        If (GetLastInputInfo(info)) Then
            Return TimeSpan.FromMilliseconds(Environment.TickCount - info.dwTime).ToString '  
        Else
            Return "N/A" 'Nothing
        End If
    End Function
End Class

Open in new window

Through testing I see that info.dwTime always returns zero.

Thoughts?
Sheldon LivingstonConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dev AdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
Read this https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getlastinputinfo

If return 0 the function fails.

Try change the definitio of LASTINPUTINFO

Private Structure LASTINPUTINFO
      Dim cbSize As Int32
      Dim dwTime As Int32
End Structure
Ron MalmsteadInformation Services ManagerCommented:
This link claims to have a working example.

https://stackoverflow.com/questions/8280799/vb-net-how-to-know-time-for-which-system-is-idle

Looks a lot like yours except this > 

Private Declare Function GetTickCount Lib "kernel32" () As Long
Sheldon LivingstonConsultantAuthor Commented:
I think the issue is with LASTINPUTINFO... it always returns zero while tick count is good.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Dev AdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
With these definition work for me:

Imports System.Runtime.InteropServices

    <StructLayout(LayoutKind.Sequential)> Structure LASTINPUTINFO
        <MarshalAs(UnmanagedType.U4)> Public cbSize As Integer
        <MarshalAs(UnmanagedType.U4)> Public dwTime As Integer
    End Structure

    <DllImport("user32.dll")>
    Shared Function GetLastInputInfo(ByRef plii As LASTINPUTINFO) As Boolean
    End Function

And I test idle time with this code:

        lastInputInf.cbSize = Marshal.SizeOf(lastInputInf)
        lastInputInf.dwTime = 0
        GetLastInputInfo(lastInputInf)
Sheldon LivingstonConsultantAuthor Commented:
It appears that this cannot be done from a service... only an app.
Dev AdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
Look at the help of the function

"This function is useful for input idle detection. However, GetLastInputInfo does not provide system-wide user input information across all running sessions. Rather, GetLastInputInfo provides session-specific user input information for only the session that invoked the function."

https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getlastinputinfo
Dev AdminSystem Engineer | .NET Developer | Microsoft MVP | Technical SpeakerCommented:
For obtain the idle time in a service you must monitoring global events look at this article:
https://www.codeproject.com/Articles/9104/How-to-check-for-user-inactivity-with-and-without

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sheldon LivingstonConsultantAuthor Commented:
Thank you Ermanno... I will be looking into this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.