title: How to set a timer in Windows application when user is idle or not active using Static Class
body: <p>I have a question on setting timer in Windows Forms when the user is idle or Inactive using Static Class and call static method. Since static will be there for the lifetime period of the exe file. I need the timer to set even on any Mouse Events. If the user makes any moment then I need to reset the timer. So this is the requirement. Here goes the code. </p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices;
  using System.Windows.Forms;
  
  namespace StaticApplicationTimeOut
  {
      public static class SetApplicationTimeOut
      {
          #region
          /// &lt;summary&gt;
          /// Private Timer Property
          /// &lt;/summary&gt;
          private static Timer _timer;
  
          /// &lt;summary&gt;
          /// Timer Property
          /// &lt;/summary&gt;
          public static Timer Timer
          {
              get
              {
                  return _timer;
              }
              set
              {
                  if (_timer != null)
                  {
                      _timer.Tick -= Timer_Tick;
                  }
  
                  _timer = value;
  
                  if (_timer != null)
                  {
                      _timer.Tick += Timer_Tick;
                  }
              }
          }
          #endregion
  
          #region Events
          public static event EventHandler UserActivity;
          #endregion
  
          #region Constructor
  
          #endregion
  
          #region Inherited Methods
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          public static void SetTimeOut()
          {
              // postpone auto-logout by 30 minutes
              _timer = new Timer
              {
                  Interval = 500 // Timer set for 30 minutes
              };
  
              Application.Idle += Application_Idle;
  
              _timer.Tick += new EventHandler(Timer_Tick);
          }
  
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          public static void StopTimer()
          {
              // Stops the timer
              _timer.Stop();
          }
  
          #endregion
  
          #region Private Methods
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          /// &lt;param name="sender"&gt;&lt;/param&gt;
          /// &lt;param name="e"&gt;&lt;/param&gt;
          private static void Application_Idle(object sender, EventArgs e)
          {
              _timer.Stop();
              _timer.Start();
          }
  
          [DllImport("user32.dll")]
          static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  
          [StructLayout(LayoutKind.Sequential)]
          struct LASTINPUTINFO
          {
              public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
  
              [MarshalAs(UnmanagedType.U4)]
              public UInt32 cbSize;
              [MarshalAs(UnmanagedType.U4)]
              public UInt32 dwTime;
          }
  
          static uint GetLastInputTime()
          {
              uint idleTime = 0;
              LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
              lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
              lastInputInfo.dwTime = 0;
  
              uint envTicks = (uint)Environment.TickCount;
  
              if (GetLastInputInfo(ref lastInputInfo))
              {
                  uint lastInputTick = lastInputInfo.dwTime;
  
                  idleTime = envTicks - lastInputTick;
              }
  
              return ((idleTime &gt; 0) ? (idleTime / 1000) : 0);
          }
  
          /// &lt;summary&gt;
          /// 
          /// &lt;/summary&gt;
          /// &lt;param name="sender"&gt;&lt;/param&gt;
          /// &lt;param name="e"&gt;&lt;/param&gt;
          private static void Timer_Tick(object sender, EventArgs e)
          {
              if (GetLastInputTime() &gt; 180)
              {
                  _timer.Stop();
                  Application.Idle -= Application_Idle;
                  MessageBox.Show("Application Terminating");
                  Application.Exit();
              }
          }
          #endregion
      }
  }
  </code></pre>
  
  <p>Like if I want to stop the timer I need to call </p>
  
  <pre><code> SetApplicationTimeOut.StopTimer();
  </code></pre>
  
  <p>and for starting the timer </p>
  
  <pre><code>SetApplicationTimeOut.SetTimeOut();
  </code></pre>
  
  <p>And if there is any moment simply it should reset the timer. How can I achieve this in static class? </p>