title: Windows Phone 8.1 App - Auto Logout (Detect if App is Running)
body: <p>I am building a Windows 8.1 Universal app, and at the minute specifically I am working on the Windows Phone side of it. The app is an RT app built using the MVVM pattern, and I am using MVVM Light.</p>
  
  <p>The app is for a financial institunion and exposes payment information. As such, there is a security requirement to log the user out of the app if there is no activity after a period of 2 minutes.</p>
  
  <p>I've searched high and low for a solution to this problem. The only one which really works is to set up gestures in the view, and use a DispatchTimer, which expires and redirects to the app lock screen (not the built-in Windows one) after 2 minutes if no gestures have been detected. A gesture resets the timer. Here is some sample code for the gesture setup:</p>
  
  <pre><code>public MainPage()
      {
          this.InitializeComponent();                      
  
          //Register the gestures, for the app timeout
          this.PointerPressed += MainPage_PointerPressed;
          this.PointerMoved += MainPage_PointerMoved;
          this.PointerReleased += MainPage_PointerReleased;
          gr.CrossSliding += Gr_CrossSliding;
          gr.Dragging += Gr_Dragging;
          gr.Holding += Gr_Holding;
          gr.ManipulationCompleted += Gr_ManipulationCompleted;
          gr.ManipulationInertiaStarting += Gr_ManipulationInertiaStarting;
          gr.ManipulationStarted += Gr_ManipulationStarted;
          gr.ManipulationUpdated += Gr_ManipulationUpdated;
          gr.RightTapped += Gr_RightTapped;
          gr.Tapped += Gr_Tapped;
          gr.GestureSettings = Windows.UI.Input.GestureSettings.ManipulationRotate | Windows.UI.Input.GestureSettings.ManipulationTranslateX | Windows.UI.Input.GestureSettings.ManipulationTranslateY |
          Windows.UI.Input.GestureSettings.ManipulationScale | Windows.UI.Input.GestureSettings.ManipulationRotateInertia | Windows.UI.Input.GestureSettings.ManipulationScaleInertia |
          Windows.UI.Input.GestureSettings.ManipulationTranslateInertia | Windows.UI.Input.GestureSettings.Tap;
  
          InitializeTimer();
      }  
  </code></pre>
  
  <p>There are methods for receiving gesture inputs on the page, like this:</p>
  
  <pre><code>private void Gr_Tapped(Windows.UI.Input.GestureRecognizer sender, Windows.UI.Input.TappedEventArgs args)
      {
          ResetTimer();
      }
  </code></pre>
  
  <p>And here is my timer code:</p>
  
  <pre><code>private DispatcherTimer _timer2 { get; set; }
      int ticks = 0;
  
      private void InitializeTimer()
      {
          _timer2 = new DispatcherTimer();
          _timer2.Tick += timer2_Tick;
          _timer2.Interval = new TimeSpan(00, 0, 1);
          bool enabled = _timer2.IsEnabled;
          _timer2.Start();
      }
  
      void timer2_Tick(object sender, object e)
      {
          ticks++;            
  
          if (ticks &gt;= 120)
          {
              Timeout();
          }
      }
  
      private void Timeout()
      {
          _timer2.Stop();
          _timer2 = null;
  
          Frame.Navigate(typeof(LockView));
      }
  
      private void ResetTimer()
      {
          _timer2.Stop();
          _timer2.Start();
  
          ticks = 0;
      }
  </code></pre>
  
  <p>This solution is far from perfect. I will have to use it if I can't find a better one, but there are 4 main problems with it;</p>
  
  <ol>
  <li>It breaks the MVVM pattern. All this code has to go in the code behind of the View. I have 7 views which are accessible after the user logs in, so there will be a lot of repitition</li>
  <li>It doesn't capture all gestures. For example is seems to ignore when scrolling up and down a listbox full of items</li>
  <li>I have to call the ResetTimer() method or _timer2.Stop() from anywhere in the view that might be considered a gesture (even just before navigating away from the view)</li>
  <li>It's just a messy and awkward solution</li>
  </ol>
  
  <p>Does anyone know of a better way of achieving this? Surely there's a more elegant solution, maybe something at a higher level, rather than trying to catch every last button press and gesture.</p>
  
  <p>Also, it would be useful if the app could detect whether it was running in the foreground or the background, and auto-navigates to the lock screen if the user navigates away from the app then goes back to it later.</p>
  
  <p>Thanks in advance for any suggestions...</p>