I want to check for app updates every 10 mins in background
What should I use optimal for this?
- Async call of void task?
- Create new thread? Or
Task.Run()
?
Precision in time isn't neccesary
I want to check for app updates every 10 mins in background
What should I use optimal for this?
Task.Run()
? Precision in time isn't neccesary
I'd use a System.Threading.Timer. You can use the Change method which takes a TimeSpan to cause it to fire every 10 minutes. The advantage of using a timer is that you're not creating a thread which is just sleeping most of the time which is inefficient. The timer is registered with the OS and while waiting for the timeout, no resources are being used.
Be aware that it is reentrant, so if your code takes more than 10 minutes it can start running again on another thread.
There's no need to worry about threads yourself with this solution, as the Timer will take a ThreadPool thread and run your callback on that. And back to my comment about your code taking longer than 10 minutes, if your call back is still running 10 minutes later, ANOTHER thread pool thread will be taken to run your timer, so in that case two threads will be running your callback concurrently.
Using a Task does not necessarily mean your code will run in another thread; it can, but its up to the scheduler to determine if its worth putting into another thread. You can still use a Task in your Timer callback as well; you might want to do this if your Task is IO bound, because when you finally await the Task, the threadpool thread will be freed to do work for other things putting requests into the pool. When your Task finishes, it will then get another (probably different) thread to continue running your timer callback code. You can find more details on how Tasks work in other answers.
You can use the Quartz.Net library. Which is a job scheduling library that you can integrate into your app and will perform your jobs following a schedule you set using CRON. Quartz uses threads off the thread pool to perform your jobs and manages these jobs for you. It's open source and has pretty good documentation.
I'll just leave the link here: https://www.quartz-scheduler.net/