Search code examples
.netwindowsmobilewindows-phone-8

Detect User Activity windows phone 8


My code to start the DispatcherTimer.

I just need to detect any kind of user Ineraction in order to restart the DispatcherTimer?

public partial class MainMenu : PhoneApplicationPage
 {
    public MainMenu()
    {
        InitializeComponent();
        startTimer();
    }

    private DispatcherTimer dispatcherTimer;
    private void startTimer()
    {
        dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        dispatcherTimer.Stop();
        AllMyFunctions.logout_code(this);
    }

    private void restartTimer()
    {
        dispatcherTimer.Stop();
        startTimer();
    }
}

how do i detect any kind of user Interaction in order to fire restartTimer() method?


Solution

  • I added an event on the Tap of every PhoneApplicationPage.

    int timeOutInSeconds;
    Timer timerObject;
    
     private void onPageLoad()
     {
       timeOutInSeconds = 30;
       timerObject = new Timer();
       timerObject.startTimer(this, timeOutInSeconds, appsettings);
     }
    private void LayoutRoot_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
       timerObject.restartTimer(timeOutInSeconds);
    } 
    
     public class Timer
    {
        public DispatcherTimer dispatcherTimer;
        public PhoneApplicationPage phoneApplicationPage;
        public IsolatedStorageSettings appsettings;
    
    
        //public Timer(PhoneApplicationPage callingPhoneApplicationPage)
        //{
        //    this.phoneApplicationPage = callingPhoneApplicationPage;
        //}
    
        public void startTimer(PhoneApplicationPage callingPhoneApplicationPage, int noOfSeconds, IsolatedStorageSettings calledAppsettings)
        {
            this.appsettings = calledAppsettings;
            this.phoneApplicationPage = callingPhoneApplicationPage;
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(timer_time_done);
            dispatcherTimer.Interval = new TimeSpan(0, 0, noOfSeconds);
            dispatcherTimer.Start();
        }
        public void timer_time_done(object sender, EventArgs e)
        {
            dispatcherTimer.Stop();
            BsgFunctions.logout_Forcefully_code(phoneApplicationPage, this, appsettings);
        }
    
        public void restartTimer(int noOfSeconds)
        {
            dispatcherTimer.Stop();
            startTimer(phoneApplicationPage, noOfSeconds,appsettings);
        }
    
        public void stopTimer()
        {
            dispatcherTimer.Stop();
        }
    
    }