Detect user activity

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?=

    Detect user activity

    Hi,

    I want an app that I'm writing to be able to hide itself whenever it hasn't
    been used for say 10 minutes.

    Is there a smart way to detect user activity within an app. Kind of overall
    message that I can hook up to so that I dont have to call some function to
    register user activity in all my gui event handlers.

    Jesper.
  • Peter Duniho

    #2
    Re: Detect user activity

    On Mon, 17 Nov 2008 03:55:01 -0800, Jesper, Denmark
    <JesperDenmark@ discussions.mic rosoft.comwrote :
    I want an app that I'm writing to be able to hide itself whenever it
    hasn't
    been used for say 10 minutes.
    >
    Is there a smart way to detect user activity within an app. Kind of
    overall
    message that I can hook up to so that I dont have to call some function
    to
    register user activity in all my gui event handlers.
    You first need to define what it means to be "used". Window messages come
    into an thread on a regular basis, even when that thread isn't actually
    being "used" by the user, at least by conventional definitions of "used".
    Repaint messages, system status messages, etc. all occur even when the
    program's own features aren't specifically being used.

    One possible approach would be to override the Control.WndProc () method,
    watching for messages like WM_KEYDOWN, WM_LBUTTONDOWN, etc. This is
    probably the most centralized approach, but it does still mean creating a
    list of messages that you consider to qualify as your program being
    "used". Alternatively, as you suggest you could of course decide in your
    program what methods are called as a result of user input, and include
    code in those methods to restart the timer when they are called.

    The latter approach is likely to match the true semantics of "used" in
    your program, but the former is probably easier to maintain and could work
    reasonably well. But again, it all depends on what your definition of
    "used" is. If you have some less-conventional definition, it might
    require yet a different approach than either of those two.

    Pete

    Comment

    • Jani Järvinen [MVP]

      #3
      Re: Detect user activity

      Hi Jesper,
      I want an app that I'm writing to be able to hide itself whenever it
      hasn't
      been used for say 10 minutes. Is there a smart way to detect user
      activity within an app?
      I'm on the same lines as Peter; you would first need to define what you mean
      by "using" your application. However, I've built several Windows
      applications that needed a similar feature, and back then I used to define
      the "using" as pressing keys and clicking the mouse. That used to be
      accurate enough for my needs.

      Depending on the application type you have (WinForms?), you could monitor
      the messages important to you, and then have a timer (say) to checked
      whether an inactivity counter has passed the ten minute limit. If it has,
      then you could do something, for example close down network/database
      connections, etc.

      In WinForms forms you have the KeyPreview property, which helps with the
      keyboard events. For the mouse I'm not aware of a similar, easy to use
      feature, but you could create an application (Win32) hook to catch the mouse
      messages centrally.

      Hope this helps!

      --
      Regards,

      Mr. Jani Järvinen
      C# MVP
      Vantaa, Finland
      janij@removethi s.dystopia.fi



      Comment

      Working...