My Requirement : Need a session timer for every page(Except login page). If timer exceeded 60 seconds with no user actions performed, it should redirect to the login page.
I have successfully done the above requirement for one page with below code snippets.
DispatcherTimer dispatcherTimer;
DateTimeOffset startTime;
DateTimeOffset lastTime;
DateTimeOffset stopTime;
int timesTicked = 1;
int timesToTick = 60;
page_load()
{
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
startTime = DateTimeOffset.Now;
lastTime = startTime;
dispatcherTimer.Start();
}
private void PhoneApplicationPage_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
{
startTime = DateTimeOffset.Now;
lastTime = startTime;
dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object sender, object e)
{
DateTimeOffset time = DateTimeOffset.Now;
TimeSpan span = time - lastTime;
lastTime = time;
timesTicked++;
if (timesTicked > timesToTick)
{
isTimeOver = true;
stopTime = time;
dispatcherTimer.Stop();
span = stopTime - startTime;
MessageBox.Show("Login again", "Session Expired", MessageBoxButton.OK);
NavigationService.Navigate(new Uri("/Login_3.xaml", UriKind.Relative));
}
}
I need this logic for every pages. As pages are not accessible in custom classes for navigation, I am confused.
I don't think writing dispatcherTimer_Tick
event in every page is good approach.
Could you please anyone advice how we can do this in one code that can be used for every page?
Thanks in advance:)