Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all

In my application what i have to do is if the user not clicking any button or the screen is idle for 10-15 sec, then it should display session expired and go back to the main form.

Please tell me how to do this.
i am using c#, windows application.

Please help me...
Posted
Updated 6-Aug-14 22:30pm
v2
Comments
Herman<T>.Instance 7-Aug-14 4:30am    
where are you stuck?
[no name] 7-Aug-14 4:31am    
there is no where to stuck..i need some idea to do this..i don't know in winforms how to do this..
Herman<T>.Instance 7-Aug-14 4:32am    
Timer object
[no name] 7-Aug-14 4:37am    
can u give me some example link..?

To do this, you need to have a program that runs in the background[^]. Try this:
C#
class Program
{
    [StructLayout(LayoutKind.Sequential)]
    struct LASTINPUTINFO
    {
        public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public UInt32 dwTime;
    }


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

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

    static void Main(string[] args)
    {
        bool running = true;
        while (running)
        {
            if (GetLastInputTime() > 60 * 1) //1 min idle time
            {
                ExitWindowsEx(0, 0);
                running = false;
                //Redirect the form here.
            }
            Thread.Sleep(1000 * 60); //check once per min.
        }
    }

    static int GetLastInputTime()
    {
        int idleTime = 0;
        LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
        lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
        lastInputInfo.dwTime = 0;

        int envTicks = Environment.TickCount;

        if (GetLastInputInfo(ref lastInputInfo))
        {
            int lastInputTick = (int)lastInputInfo.dwTime;

            idleTime = envTicks - lastInputTick;
        }

        return ((idleTime > 0) ? (idleTime / 1000) : 0);
    }

}


You can also refer Retrieving the Operating System Idle Time, Uptime and Last Input Time[^] for more information.

--Amy
 
Share this answer
 
Comments
[no name] 7-Aug-14 7:57am    
How to use this code in my application.
i kept it in my program.cs file but it is not allowing to run the pages..
how to use this as background..??
If my any form is idle for 1 min, it should redirect to main form.
Please try this and let me know :

Response.AddHeader("REFRESH","10;URL=secondpage.aspx");
 
Share this answer
 
Comments
[no name] 7-Aug-14 4:31am    
I am working on winforms..
SRK90 7-Aug-14 4:37am    
Please refer the below link :

http://stackoverflow.com/questions/23250224/windows-forms-wait-5-seconds-before-displaying-a-message


Please let me know your update !!!

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