I am building cross platform apps with Xamarin and MvvmCross. I need to call the server to the updates every minute (I will move to push notifications later) but I am unable to make a timer in my Core Project. I've seen MvvmCross N+42 but I believe the target projects are older which allows the timer. Below is my target framework.
Is there a better way for me to constantly call a method which calls a service?
- .NET Framework 4.5 and Higher
- Windows Store apps (Windows 8) and higher
- Windows Phone 8
- Xamarin.Android
- Xamarin.iOS
You can always do it yourself thanks to the wonder of async/await:
When you call it, don't await it. It will run on a background thread and you can terminate the loop by setting the _timerRunning field from elsewhere (or by killing the Task that's returned from the call).
@stevemorgan answer works really well. I created a Timer utility based on that code to make it more reusable. I also added a "runOnce" parameter that will stop the timer after the first tick
I´m using this in MvvmCross with no issues: