Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to raise an event at any given time

I'm trying to raise an event at a given time in my windows store app. Now I've done this in desktop apps countless times, and I've used System.Threading.Timer in the past and it has worked well, but that class is not available to windows store apps.

I have looked in the documentation and found a class called DispatchTimer and although it appears to be what I'm after, correct me if I'm wrong but the docs are lacking. But luckily it's pretty easy to use.

So I tried the DispatchTimer, but after using it, I'm not even sure this is what I should be using.

How can I watch for any given time and raise an event when that time is up (in a windows store app)? And do you know of any resources that do this in a metro app?

like image 436
Arrow Avatar asked Nov 19 '25 16:11

Arrow


2 Answers

Use DispatcherTimer like this:

var timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(10) };
timer.Tick += OnTimerTick;
timer.Start();

private void OnTimerTick(object sender, object args)
{
    // Do something with pickup here...
}

This will create a timer with intervals of 10 seconds.

like image 142
khellang Avatar answered Nov 22 '25 06:11

khellang


The DispatcherTimer is the way to go. Notice that if you want your app to run in background you must declare that on the app manifest or use Background agents.

like image 26
DVD Avatar answered Nov 22 '25 07:11

DVD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!