I have an application in which I have multiple slow running prerequisite methods I need to call in order to have the data I need to continue.
To try to speed this up I am doing this:
Dictionary<string, string> processOptions = null;
Dictionary<string, string> businessAreas = null;
Dictionary<string, string> requirementLevels = null;
List<TemplateSection> sections = null;
List<TemplateVersion> versions = null;
var prerequisiteTasks = new List<System.Threading.Tasks.Task>();
prerequisiteTasks.Add(new System.Threading.Tasks.Task(() => sections = GetSectionsInTreeForm(latestVersionGuid)));
prerequisiteTasks.Add(new System.Threading.Tasks.Task(() => versions = GetTemplateVersions(templateId)));
prerequisiteTasks.Add(new System.Threading.Tasks.Task(() => processOptions = GetOptionSetProcessPhaseTypes()));
prerequisiteTasks.Add(new System.Threading.Tasks.Task(() => businessAreas = GetOptionSetBusinessAreaTypes()));
prerequisiteTasks.Add(new System.Threading.Tasks.Task(() => requirementLevels = GetOptionSetRequirementLevelTypes()));
prerequisiteTasks.ForEach(x => x.Start());
System.Threading.Tasks.Task.WaitAll(prerequisiteTasks.ToArray());
Each of the methods above can take anything from 2-5 seconds to complete. Is there a nicer way of doing this, or is this something I shouldent do?
P.S. the code as is works great and decreases the time used by around 30%.