public void loadScheduleTask() { string[] taskFiles = FileUtils.getAllScheduleFile(); scheduleTaskListView.Items.Clear(); 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() }; ListViewItem item = new ListViewItem(row); // Add the Item scheduleTaskListView.Invoke(new Action(() => scheduleTaskListView.Items.Add(item))); } } catch { } }
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(); } }