I have an application that dynamically loads and runs DLLs writen by users. It has a scheduling engine that runs on a timer and checks which "rules" (dlls) to run. Each rule is started on its own thread using:
private void OnTimer(...)
{
timer.stop();
...
foreach(RuleData ruleData in RulesToRun)
ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ExecuteRule), ruleData);
_asyncOpsAreDone.WaitOne();
timer.start();
}
and when the async operations are done, the program waits for the schedualing timer to tick and run another loop.
Problem is that sometimes users write dlls that take a lot of time to run.
- If I'll wait: This will postpone the next timer tick (as it is stoped while executing and started again after)
- If I wont wait: I'm looking for unknown number of rules running in parallel, finishing my pool of application users.
If you would take into consideration that one rule execution might hang, My choise is between: Synch - hang all rules, or Asynch - dry resource pools.
I seriously thought about giving rule execution a timeout, but .Abort() isn't so popular: Timeout Pattern - How bad is Thread.Abort really?
And Parallel progrming solution for Cancelation of task, Involves writing "ThrowIfCancellationRequested();" in the dll execution (which I don't have source code for) http://msdn.microsoft.com/en-us/library/dd997396.aspx