private void LaunchTask(TaskLaunchInfo l) { ProcessWrapper wrap = new ProcessWrapper(l.schedule); wrap.Complete += new ProcessWrapper.ProcessCompleteEventHandler(wrap_Complete); Thread t = new Thread(wrap.Go); _threads.Add(t, wrap); t.Start(); }
private void ServiceThreadRun() { lock (pauseMutex) { while (!_stop) { TimeSpan pause = new TimeSpan(0, 10, 0); try { //launch pending tasks DateTime now = DateTime.Now; for (int i = 0; i < _taskInfo.Count; i++) { if (_taskInfo[i].launchTime < now) { LaunchTask(_taskInfo[i]); TaskLaunchInfo l = _taskInfo[i]; l.launchTime = GetNextLaunchTime(_taskInfo[i].schedule); } } //compute pause now = DateTime.Now; pause = new TimeSpan(100, 0, 0, 0, 0); foreach (TaskLaunchInfo l in _taskInfo) { TimeSpan ti = (l.launchTime - now); if (ti < pause) { pause = ti; } } } catch (Exception ex) { Program.WriteEvent("Error launching tasks: " + ex.Message, EventLogEntryType.Error); } //now, wait for that min time if (pause > new TimeSpan(0)) { Monitor.Wait(pauseMutex, pause); } } _serviceThread = null; //we're meeeeeeeeelting...... //kill all the processes in the running threads foreach (ProcessWrapper p in _threads.Values) { p.Kill(); } Thread.Sleep(3000); Monitor.PulseAll(pauseMutex); Stop(); } }
protected override void OnStart(string[] args) { _threads = new Dictionary <Thread, ProcessWrapper>(); _taskInfo = new List <TaskLaunchInfo>(); try { //read in the config Tasks t = new Tasks(); string configFile = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; configFile = configFile.Substring(0, configFile.LastIndexOf('/')) + "/Config.xml"; t.ReadXml(configFile); if (t.Task == null || t.Task.Rows == null || t.Task.Rows.Count == 0) { throw new Exception("No tasks found"); } if (t.Schedule == null || t.Schedule.Rows == null || t.Schedule.Rows.Count == 0) { throw new Exception("No schedules found"); } //compute next launch time for each task foreach (Tasks.ScheduleRow row in t.Schedule) { DateTime nextLaunch = GetNextLaunchTime(row); TaskLaunchInfo l = new TaskLaunchInfo(); l.launchTime = nextLaunch; l.schedule = row; _taskInfo.Add(l); } } catch (Exception ex) { Program.WriteEvent("Error reading config: " + ex.Message + Environment.NewLine + ex.StackTrace, EventLogEntryType.Error); Stop(); throw; } _serviceThread = new Thread(ServiceThreadRun); _serviceThread.Start(); }