In the previous question you gave an excellent description on what a Hook is.
1. How are Scheduled Tasks different from Hooks?
Hooks are specific pieces of code that are loaded into the worker process and generally attach to some sort of event that occurs in the system. One of the most common events that Hooks utilize is the
HeartBeat static class which in it's own right runs very similarly to the Scheduler spawning a continuous loop. The Heartbeat events are subscribed to by Hooks through the
AlarmClock class. When the
HeartBeat beats a tick, if the AlarmClock interval is reached, it invokes all of it's event subscribers by instantiating the class for that beat/interval, executing their delegate method, and then disposing.
The Scheduler is not much different to some degree from the
HeartBeat static class. It too is a static class that when instantiated, spawns a Worker thread that continuously loops. The biggest difference though is that the Scheduler loads into memory all of the Agent objects together as array and instead of subscribing to events, checks a time interval of each agent. When the time interval has passed, then it executes the agent.
The other big difference between a Hook and Scheduled Task/Agent is that you have to use the Agent class mechanism in a Scheduler task. A Hook can be any type of custom code, logic, or method and not confined to the rigor of an Agent.
2. When would I use a Scheduled Task over a Hook?
Scheduled tasks are meant for small pieces of work that are intended to be executed for a specific purpose and if turned off are not going to hurt or jeopardize the overall function of the product. An example is the
CleanupEventQueue task. It does one function and gets out. If this function were to be disabled, chance of a major Sitecore malfunction are low.
3. When would I use a Hook over a Scheduled Task?
When more core, and possibly more important functionality needs to be executed that would not be affected by user intervention. Hooks are separated from the Scheduler configuration and therefore the likely hood of a hook being modified or interfered with by a developer or other factor is much lower. An great example of a hook being chosen over a Scheduled Task is actually the
<hook type="Sitecore.ListManagement.Analytics.Hooks.UnlockContactListsAgentLoader, Sitecore.ListManagement.Analytics" patch:source="Sitecore.ListManagement.config">
<param name="agent" type="Sitecore.ListManagement.Analytics.UnlockContactListsAgent, Sitecore.ListManagement.Analytics">
<param name="listManager" ref="contactListManager" />
<param name="mapper" type="Sitecore.ListManagement.ContentSearch.BatchIdMapper, Sitecore.ListManagement.ContentSearch" />
<param name="operationManager" type="Sitecore.Analytics.Data.Bulk.Contact.ContactBulkUpdateManager, Sitecore.Analytics" />
Here we have a hook that is being initialized, and it's actually creating an Agent Task to run on a specific interval. The reason why Sitecore did this (assumption) is that this is a process that would have detrimental effect if the scheduler was turned off or disabled. If Lists were to be created and not unlocked (There is a side conversation here about a bug that is preventing list from getting unlocked, but that's for another time) this would pose a significant issue to the operation of Sitecore. Especially if EXM was in play. :wink:
As far as Hooks and Scheduled Tasks are concerned, I see them as cousins in a big family tree. The only difference is that the parents of Hooks are more strict and less likely to be swayed by adolescent rage. Where as Schedule Tasks parents are more laid back and subdue. They probably let the kids play out in less than ideal conditions.
To put it in short terms:
If it's important use a Hook.
If it's for convenience use a Scheduled Task.