public static ScheduleTask getTaskFromFile(string fileName) { try { string[] lines = File.ReadAllLines(fileName); ScheduleTask task = new ScheduleTask(lines[0], lines[1], lines[2]); task.setDelayStart(double.Parse(lines[3])); task.setInterval(double.Parse(lines[4])); task.setRepeat(bool.Parse(lines[5])); return(task); } catch { return(null); } }
public static bool appendToScheduleFile(string fileName, ScheduleTask task) { try { createDirectoryIfNotExist(); string filePath = schedulePath + "\\" + fileName + ".txt"; string[] lines = { task.getTaskName(), task.getTaskDescription(), task.getTaskFileName(), task.getDelayStartTotalSeconds().ToString(), task.getIntervalTotalSeconds().ToString(), task.getIsRepeat().ToString() }; File.WriteAllLines(filePath, lines); } catch { return(false); } return(true); }
private void applyTask( CancellationToken cancellationToken, CountdownEvent countdownEvent ) { string[] taskFiles = FileUtils.getAllScheduleFile(); List <System.Threading.Timer> timers = new List <System.Threading.Timer>(); CountdownEvent c = new CountdownEvent(1); countdownEvent.Reset(); try { foreach (string file in taskFiles) { ScheduleTask task = FileUtils.getTaskFromFile(file); cancellationToken.ThrowIfCancellationRequested(); string[] row = { task.getTaskName(), task.getTaskDescription(), task.getTaskFileName(), task.getDelayStartTotalSeconds().ToString(), task.getIntervalTotalSeconds().ToString(), task.getIsRepeat().ToString() }; if (task.getIsRepeat()) { System.Threading.Timer timer = new System.Threading.Timer( x => { Process.Start(task.getTaskFileName()); //c.Signal(1); }, null, task.getDelayStart(), task.getInterval() ); timers.Add(timer); } else { System.Threading.Timer timer = new System.Threading.Timer( x => { Process.Start(task.getTaskFileName()); //c.Signal(1); }, null, (int)task.getDelayStart().TotalMilliseconds, System.Threading.Timeout.Infinite ); timers.Add(timer); } } countdownEvent.Wait(cancellationToken); } catch { Debug.WriteLine("Cancel schedule"); foreach (System.Threading.Timer timer in timers) { timer.Dispose(); } timers.Clear(); } }