title: How to detect a Winforms app has been idle for certain amount of time
body: <p>What is the best way, to detect if a C# Winforms application has been idle for a certain period of times?</p>
  
  <p>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.</p>
  
  <p>This is the accepted answer for a similar question:
  <a href="https://stackoverflow.com/questions/1541981/check-if-an-application-is-idle-for-a-time-period-and-lock-it">Check if an application is idle for a time period and lock it</a></p>
  
  <p>However, the answer is pertinent to Windows being idle for a period of time, <strong>not</strong> a specific application. I want our application to terminate if it's not been used for say, 30 minutes.</p>
  
  <p>I looked at this:</p>
  
  <p><a href="http://www.codeproject.com/Articles/13756/Detecting-Application-Idleness" rel="noreferrer">http://www.codeproject.com/Articles/13756/Detecting-Application-Idleness</a></p>
  
  <p>However I read in the comments that this doesn't work for multi-threaded applications, of which our app is one. <strong>Our app has a main form, which spawns modal and modeless forms, which use Async Await to fill grids etc.</strong></p>
  
  <p>Then I looked at SetWindowsHookEx, unsure if that would work.</p>
  
  <p>Surely someone has a solution (compatible with .NET 4.5 hopefully ) :)</p>
  
  <p>TIA</p>