You must Sign In to post a response.
  • Category: Visual Studio

    Finding System Idle Time and Log off Session

    Hi all
    I am using desktop application (VB.NET and SQL Server 2005) . I need a sample code to
    find the System idle time and after 15 mins if the application is idle it will exit the application to
    login screen

    Thanks and Regards
    Sankar
  • #765762
    with the help of inbuilt functions like 'GetLastInputInfo' we can get the system idle time, see below snippet

    using System;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Helps to find the idle time, (in ticks) spent since the last user input
    /// </summary>
    public class IdleTimeFinder
    {
    [DllImport("User32.dll")]
    private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

    [DllImport("Kernel32.dll")]
    private static extern uint GetLastError();

    public static uint GetIdleTime()
    {
    LASTINPUTINFO lastInPut = new LASTINPUTINFO();
    lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
    GetLastInputInfo(ref lastInPut);

    return ((uint)Environment.TickCount - lastInPut.dwTime);
    }
    /// <summary>
    /// Get the Last input time in ticks
    /// </summary>
    /// <returns></returns>
    public static long GetLastInputTime()
    {
    LASTINPUTINFO lastInPut = new LASTINPUTINFO();
    lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
    if (!GetLastInputInfo(ref lastInPut))
    {
    throw new Exception(GetLastError().ToString());
    }
    return lastInPut.dwTime;
    }
    }


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #765768
    How to exit the application to Login screen please mention

  • #765769
    Use Environment.Exit(1) to exist the application.
    If you want to redirect to login page then directly create object of login form and open it

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments