20

I'm working on an UWP MVVM project and would like to implement an automatic logout system if the user interaction stops for a specific time.
Until now I'm using a DispatcherTimer to count backwards from 200 every second.

TimerLeave = 200;
var _dispatcherTimer = new DispatcherTimer();
_dispatcherTimer.Tick += dispatcherTimer_Tick;
_dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

_dispatcherTimer.Start();

But because the DispatcherTimer is linked with the UI and I'm building a MVVM App, I'm looking for an alternative.
I searched a bit and found Run a background task on a timer. The problem is that this timer can only be set to run every 15 minutes, which is a little too long to automaticly logout a user in my case. I found no workaround to reduce the 15 minutes.
So my question is, is there any possibility to set up a timer in an UWP Project that isn't linked to the UI and can be set variable?

croxy
  • 4,082
  • 9
  • 28
  • 46
  • 1
    did you check this: [link](http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer) ? – Antonio Ugraal Barile Dec 14 '15 at 15:51
  • 1
    @AntonioUgraalBarile I totally missed System.Threading.Timer, thank you for this hint. I've only found System.Timer and this one isn't supported in UWP Apps. – croxy Dec 14 '15 at 15:55

3 Answers3

26

Yes - you can for example use Timer class - though you must remember that it run on separate thread. Example:

private Timer timer;
public MainPage()
{        
    this.InitializeComponent();
    timer = new Timer(timerCallback, null, (int)TimeSpan.FromMinutes(1).TotalMilliseconds, Timeout.Infinite);
}

private async void timerCallback(object state)
{
    // do some work not connected with UI

    await Window.Current.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
        () => {
            // do some work on UI here;
        });
}

Note that the work dispatched on UI dispatcher may not be processed right away - it depend on dispatcher's workload.

Also remember that this timer runs along with your app and won't work when app is suspended.

Mike Keskinov
  • 11,614
  • 6
  • 59
  • 87
Romasz
  • 29,662
  • 13
  • 79
  • 154
12

I resolved this issue recently using a ThreadPoolTimer class.

ThreadPoolTimer timer = ThreadPoolTimer.CreatePeriodicTimer((t) =>
        {
            //do some work \ dispatch to UI thread as needed
        }, TimeSpan.FromMinutes(1));

At present I am not aware of any advantage of this over the Timer solution already posted, but it has functioned well.

Saxar
  • 474
  • 5
  • 5
  • 2
    Tested both solutions.ThreadPoolTimer is more accurate than Timer. Will use it. – x2. Aug 11 '16 at 10:17
0

You may be right at your code but I have used threadpool timer instead of dispacher timer, after referring above blog.

If I talk about initial level I was using dispatcher timer for my app, what happens actually, it goes hang after every 2days of continuous standby. So I thought of threadpool timer as you are referring in this blog, but in threadpool it went hung in just 5-6 hours.

So as per my experience I think dispatcher timer is more preferable than threadpool timer.

It's just my experience of using it.

Zoe
  • 27,060
  • 21
  • 118
  • 148
siddharth
  • 1
  • 1
  • Can you turn this into an answer to the question at the top of this page? This is not a blog, it is a Q/A community. So you are supposed to answer questions when you start typing into the appropriatly labeled "Your Answer" field. – Yunnosch Mar 28 '19 at 06:45