If you have a recurring task that runs once per day, you use a Sc开发者_StackOverflow社区heduled Task.
If you have a recurring task that runs every 10 seconds, you use a Service.At what point do you switch between the two? Is there official guidance on this somewhere?
i`m not sure the interval is the main issue here. here are a few thing to consider:
- how much state this task needs in memory - do you load stuff from a file of DB ?
- does the system that needs this task to run, have a need to communicate with the task other that when its running ?
- do you need more control over the process lifecycle when the task is up?
you can see where i`m going with this , that a service is a resident entity, and a sched task isn't.
i think it depends on the point if your programm is made for only one task or for more. if it's just doin' one "stupid" thing (like running a stored procedure in a database every 20 seconds) i would concidering a sheduled task, but if it does more than that and maybe got some dependencies (maybe what time it is running or some file-operations) I would concider a service. I would also concider a service if the intervals when the operation is made are different. Let's say your programm runs a single stored procedure in a database and depending on the fact that it made "real" changes to the db. If it did something the next run is in 5 seconds and if not the next run is in 20 seconds. That's one of the perfect examples for a service.
精彩评论