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 { } }
private void addTaskButton_Click(object sender, EventArgs e) { AddTaskForm form = new AddTaskForm(); form.ShowDialog(); if (form.isSuccess) { ScheduleTask task = new ScheduleTask( form.taskNameResult, form.taskDescResult, form.taskFileResult ); task.setDelayStart(form.delayStart.TotalSeconds); task.setInterval(form.timeInterval.TotalSeconds); task.setRepeat(form.isRepeat); FileUtils.appendToScheduleFile(task.getTaskName(), task); MessageBox.Show("Add task succesfully"); loadScheduleTask(); } DateTime now = DateTime.Now; //Process.Start("C:\\Users\\Nhu Hoang\\Desktop\\build.txt"); DateTime firstRun = new DateTime(now.Year, now.Month, now.Day, now.Hour, 54, 0, 0); //Debug.WriteLine(); //c.Wait(); //Debug.WriteLine(FileUtils.getTaskFromFile(FileUtils.getAllScheduleFile()[0]).getDelayStartTotalSeconds().ToString()); }
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(); } }