public PathFromAndTo() { From = new PathCredentials(); To = new PathCredentials(); IsEnabled = true; }
public PathFromAndTo(PathCredentials from, PathCredentials to) { From = from; To = to; IsEnabled = true; }
private void timer1_Tick(object sender) { try { var task = ServiceConfig.Default.GetTask(fromPath.Path, toPath.Path); if (task != null) { if (!task.IsEnabled) //first check if task become disabled { Stop(); IsRunning = false; return; } fromPath = task.From; toPath = task.To; Schduletask = task.ScheduleTask; } bool CanRunNow = false, MustStop = false; if (Schduletask == null || !Schduletask.IsEnabled) { if (IsRunning) { return; } CanRunNow = true; } else if (Schduletask != null && Schduletask.IsEnabled) { if (Schduletask.EndTime.HasValue)// if schduled enabled then check the end time { switch (Schduletask.EndTime_Type) { case EndOnType.SameStartTime: if (!LastRunOn.HasValue && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; case EndOnType.NextDay: if (!LastRunOn.HasValue && DateTime.Now.Date >= Schduletask.EndTime.Value.Date.AddDays(1) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date.AddDays(1) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; case EndOnType.After_2_days: if (!LastRunOn.HasValue && DateTime.Now.Date >= Schduletask.EndTime.Value.Date.AddDays(2) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date.AddDays(2) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; case EndOnType.After_3_days: if (!LastRunOn.HasValue && DateTime.Now.Date >= Schduletask.EndTime.Value.Date.AddDays(3) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date.AddDays(3) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; case EndOnType.After_5_days: if (!LastRunOn.HasValue && DateTime.Now.Date >= Schduletask.EndTime.Value.Date.AddDays(5) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date.AddDays(5) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; case EndOnType.After_7_days: if (!LastRunOn.HasValue && DateTime.Now.Date >= Schduletask.EndTime.Value.Date.AddDays(7) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) //didn't run yet { MustStop = true; } else if (LastRunOn.HasValue && DateTime.Now.Date >= LastRunOn.Value.Date.AddDays(7) && Schduletask.EndTime.Value.TimeOfDay <= DateTime.Now.TimeOfDay) { MustStop = true; } break; default: break; } if (MustStop) { Stop(); return; } } switch (Schduletask.Triggertype) { case TriggerType.Daily: if (DateTime.Now >= Schduletask.StartTime) { if (!LastRunOn.HasValue) { CanRunNow = true; } else if (LastRunOn.HasValue && LastRunOn.Value.Date != DateTime.Now.Date) { CanRunNow = true; } } break; case TriggerType.Weekly: if (DateTime.Now.DayOfWeek == Schduletask.StartTime.DayOfWeek && (DateTime.Now.Hour >= Schduletask.StartTime.Hour || (DateTime.Now.Hour == Schduletask.StartTime.Hour && DateTime.Now.Minute >= Schduletask.StartTime.Minute))) { CanRunNow = true; } break; case TriggerType.Monthly: if (DateTime.Now.Day == Schduletask.StartTime.Day && (DateTime.Now.Hour >= Schduletask.StartTime.Hour || (DateTime.Now.Hour == Schduletask.StartTime.Hour && DateTime.Now.Minute >= Schduletask.StartTime.Minute))) { CanRunNow = true; } break; default: break; } } if (CanRunNow && !IsRunning) { thrd = new Thread(RunRoboCopyProcess); thrd.Start(); } } catch (Exception er) { ErrorOccured?.Invoke(this, er); } finally { // have the timer starts in 1 second, and then fire once every min timer1.Change(TIME_INTERVAL_IN_MILLISECONDS, Timeout.Infinite); } }