How to detect a Winforms app has been idle for certain amount of time

Scott

What is the best way, to detect if a C# Winforms application has been idle for a certain period of times?

If a user decides to ALT+TAB and do some work with Microsoft Word or whatever for 30 minutes, leaving our app unused, I want our app to self-terminate.

This is the accepted answer for a similar question: Check if an application is idle for a time period and lock it

However, the answer is pertinent to Windows being idle for a period of time, not a specific application. I want our application to terminate if it's not been used for say, 30 minutes.

I looked at this:

http://www.codeproject.com/Articles/13756/Detecting-Application-Idleness

However I read in the comments that this doesn't work for multi-threaded applications, of which our app is one. Our app has a main form, which spawns modal and modeless forms, which use Async Await to fill grids etc.

Then I looked at SetWindowsHookEx, unsure if that would work.

Surely someone has a solution (compatible with .NET 4.5 hopefully ) :)

TIA

user34660

There are many ways to do it and the answer somewhat depends on what you need to do. You are clear and specific about what you need. The following is something I developed that probably would fit your requirements. What it is doing is using Application.Idle to determine when the application has finished processing messages then it sets a timer and filters (listens to) all messages for the application and if a relevant message (such as mouse or keyboard) is received then it resets the timer. It ignores mouse move since it is possible to move the mouse over the application without using the application. It has been a while since I wrote that so I am not sure of the details but I could figure it out if necessary. Note that this is a console program to make the sample easier to try but the code is intended for a forms application.

using System;
using System.Security.Permissions;
using System.Windows.Forms;

namespace _121414
{
    static class Program
    {
        public static Timer IdleTimer = new Timer();
        const int MinuteMicroseconds = 60000;
        static Form1 f = null;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            LeaveIdleMessageFilter limf = new LeaveIdleMessageFilter();
            Application.AddMessageFilter(limf);
            Application.Idle += new EventHandler(Application_Idle);
            IdleTimer.Interval = MinuteMicroseconds;    // One minute; change as needed
            IdleTimer.Tick += TimeDone;
            IdleTimer.Start();
            f = new Form1();
            Application.Run(f);
            Application.Idle -= new EventHandler(Application_Idle);
        }

        static private void Application_Idle(Object sender, EventArgs e)
        {
            if (!IdleTimer.Enabled)     // not yet idling?
                IdleTimer.Start();
        }

        static private void TimeDone(object sender, EventArgs e)
        {
            IdleTimer.Stop();   // not really necessary
            MessageBox.Show("Auto logoff");
            f.Close();
        }

    }

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
    public class LeaveIdleMessageFilter : IMessageFilter
    {
        const int WM_NCLBUTTONDOWN = 0x00A1;
        const int WM_NCLBUTTONUP = 0x00A2;
        const int WM_NCRBUTTONDOWN = 0x00A4;
        const int WM_NCRBUTTONUP = 0x00A5;
        const int WM_NCMBUTTONDOWN = 0x00A7;
        const int WM_NCMBUTTONUP = 0x00A8;
        const int WM_NCXBUTTONDOWN = 0x00AB;
        const int WM_NCXBUTTONUP = 0x00AC;
        const int WM_KEYDOWN = 0x0100;
        const int WM_KEYUP = 0x0101;
        const int WM_MOUSEMOVE = 0x0200;
        const int WM_LBUTTONDOWN = 0x0201;
        const int WM_LBUTTONUP = 0x0202;
        const int WM_RBUTTONDOWN = 0x0204;
        const int WM_RBUTTONUP = 0x0205;
        const int WM_MBUTTONDOWN = 0x0207;
        const int WM_MBUTTONUP = 0x0208;
        const int WM_XBUTTONDOWN = 0x020B;
        const int WM_XBUTTONUP = 0x020C;

        // The Messages array must be sorted due to use of Array.BinarySearch
        static int[] Messages = new int[] {WM_NCLBUTTONDOWN,
            WM_NCLBUTTONUP, WM_NCRBUTTONDOWN, WM_NCRBUTTONUP, WM_NCMBUTTONDOWN,
            WM_NCMBUTTONUP, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, WM_KEYDOWN, WM_KEYUP,
            WM_LBUTTONDOWN, WM_LBUTTONUP, WM_RBUTTONDOWN, WM_RBUTTONUP,
            WM_MBUTTONDOWN, WM_MBUTTONUP, WM_XBUTTONDOWN, WM_XBUTTONUP};

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_MOUSEMOVE)  // mouse move is high volume
                return false;
            if (!Program.IdleTimer.Enabled)     // idling?
                return false;           // No
            if (Array.BinarySearch(Messages, m.Msg) >= 0)
                Program.IdleTimer.Stop();
            return false;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

iOS:Convert ObjC code to C#, How to know the time app has been idle

From Dev

How to know if a certain amount of time has passed while user is using my app?

From Dev

Meteor - How can I detect if an app has been opened?

From Dev

How to detect when android app has been upgraded?

From Dev

How kill / stop an app in background after a certain amount of time

From Dev

Detect which app has been launched in android

From Dev

How to detect if mouse button is held down for a certain amount of time after click

From Dev

How can I detect if a certain post on a Facebook page has been deleted?

From Dev

How to detect if WinForms Panel has scrolled to the end?

From Dev

How to spawn a 'boss' after a certain amount of time has passed and then kill that 'boss'?

From Dev

WPF how to detect listviewitem has been selected

From Dev

How to detect if an object variable has been changed?

From Dev

How to detect a file has been deleted

From Dev

How to detect if CurrentCulture has been customised by the user?

From Dev

How to detect a file has been deleted

From Dev

WPF how to detect listviewitem has been selected

From Java

How to change an integer every certain amount of time

From Dev

How can I detect if an Angular app has been initialized from outside Angular?

From Dev

How to detect if app has been "minimized" by home button or hidden behind another activity

From Dev

How can I detect if an Angular app has been initialized from outside Angular?

From Dev

How to detect Android App has been upgraded from version x to y?

From Dev

How to know if iOS app hasn't received a touch event for a certain amount of time

From Dev

Disable a certain text input that has been created dynamically if the length is over a certain amount of chars(VUE.JS)

From Dev

How to check if a certain method has been called?

From Dev

Advice needed for sql query. Need to count the amount of time a song has been played in a festival and how many times it won during the finals

From Dev

Bash - How to detect if a variable is more than a certain amount of characters

From Dev

Can an app detect that a file (image) has been copied off the device?

From Dev

How to detect if mouse has clicked inside of a certain shape in c# on winform app?

From Dev

How can a script detect a user's idle time?

Related Related

  1. 1

    iOS:Convert ObjC code to C#, How to know the time app has been idle

  2. 2

    How to know if a certain amount of time has passed while user is using my app?

  3. 3

    Meteor - How can I detect if an app has been opened?

  4. 4

    How to detect when android app has been upgraded?

  5. 5

    How kill / stop an app in background after a certain amount of time

  6. 6

    Detect which app has been launched in android

  7. 7

    How to detect if mouse button is held down for a certain amount of time after click

  8. 8

    How can I detect if a certain post on a Facebook page has been deleted?

  9. 9

    How to detect if WinForms Panel has scrolled to the end?

  10. 10

    How to spawn a 'boss' after a certain amount of time has passed and then kill that 'boss'?

  11. 11

    WPF how to detect listviewitem has been selected

  12. 12

    How to detect if an object variable has been changed?

  13. 13

    How to detect a file has been deleted

  14. 14

    How to detect if CurrentCulture has been customised by the user?

  15. 15

    How to detect a file has been deleted

  16. 16

    WPF how to detect listviewitem has been selected

  17. 17

    How to change an integer every certain amount of time

  18. 18

    How can I detect if an Angular app has been initialized from outside Angular?

  19. 19

    How to detect if app has been "minimized" by home button or hidden behind another activity

  20. 20

    How can I detect if an Angular app has been initialized from outside Angular?

  21. 21

    How to detect Android App has been upgraded from version x to y?

  22. 22

    How to know if iOS app hasn't received a touch event for a certain amount of time

  23. 23

    Disable a certain text input that has been created dynamically if the length is over a certain amount of chars(VUE.JS)

  24. 24

    How to check if a certain method has been called?

  25. 25

    Advice needed for sql query. Need to count the amount of time a song has been played in a festival and how many times it won during the finals

  26. 26

    Bash - How to detect if a variable is more than a certain amount of characters

  27. 27

    Can an app detect that a file (image) has been copied off the device?

  28. 28

    How to detect if mouse has clicked inside of a certain shape in c# on winform app?

  29. 29

    How can a script detect a user's idle time?

HotTag

Archive