-1

I want to check for app updates every 10 mins in background

What should I use optimal for this?

  1. Async call of void task?
  2. Create new thread? Or
  3. Task.Run()?

Precision in time isn't neccesary

Andrew
  • 129
  • 9

2 Answers2

1

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.

Andy
  • 8,432
  • 6
  • 38
  • 76
  • Nice. You might want to add that a timer doesn't generally have any impact on the number of running threads and that timers are better than a thread sitting around doing nothing for 10 minutes. :) –  Nov 08 '18 at 03:19
  • @Andy Excellent! I will use timer! But still interested in what better - start thread or task? – Andrew Nov 08 '18 at 03:41
  • @Andrew when idling, a timer might not require a thread at all and if it does it will be multiplexed. However, spinning up an explicit thread that does nothing but `Thread.Sleep(TimeSpan.FromMinutes(10))` is quite inefficient. 10 minutes is a long time in the scheme of things. There are plenty of things that expensive thread could have been doing in the interim –  Nov 08 '18 at 04:00
  • @MickyD Thanks! – Andrew Nov 08 '18 at 09:50
  • @MickyD I tried to incorporate your feedback; let me know what you think. – Andy Nov 08 '18 at 14:01
  • @Andrew I tried to add some more discussion about Task and why you might still use one in your timer callback; HTH – Andy Nov 08 '18 at 14:02
  • @Andy Thanks again! – Andrew Nov 08 '18 at 15:30
0

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/

  • Might want to say why you chose Quartz.net over say Windows Task Scheduler. https://stackoverflow.com/questions/5115105/quartz-net-vs-windows-scheduled-tasks-how-different-are-they –  Nov 08 '18 at 04:04