Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum IntervaloEnum
- {
- InicioJornada,
- InicioIntervalo,
- FimIntervalo,
- FimJornada
- }
- public class Intervalo
- {
- public int Hora { get; set; }
- public int Minutos { get; set; }
- public int Segundos { get; set; }
- public IntervaloEnum Descricao { get; set; }
- private DateTime TimeNow { get
- {
- return DateTime.Now;
- }
- }
- public DateTime Data
- {
- get{
- return new DateTime(TimeNow.Year, TimeNow.Month, TimeNow.Day, Hora, Minutos, Segundos);
- }
- }
- public List<Intervalo> ListTime()
- {
- var rnd = new Random();
- var list = new List<Intervalo>
- {
- new Intervalo{
- Hora = rnd.Next(9, 11),
- Minutos = (Hora == 9)? rnd.Next(1,59) : rnd.Next(1,10),
- Segundos = rnd.Next(1,59),
- Descricao = IntervaloEnum.InicioJornada
- },
- new Intervalo
- {
- Hora = rnd.Next(12,14),
- Minutos = (Hora == 12)? rnd.Next(1,59) : rnd.Next(1,10),
- Segundos = rnd.Next(1,59),
- Descricao = IntervaloEnum.InicioIntervalo
- },
- new Intervalo
- {
- Hora = 13,
- Minutos = (Hora == 12)? rnd.Next(1,59) : rnd.Next(1,30),
- Segundos = rnd.Next(1,59),
- Descricao = IntervaloEnum.FimIntervalo
- },
- new Intervalo
- {
- Hora = rnd.Next(18,19),
- Minutos = (Hora == 18)? rnd.Next(1,59) : rnd.Next(1,30),
- Segundos = rnd.Next(1,59),
- Descricao = IntervaloEnum.FimJornada
- }
- };
- var modificadorFimJornada = list.Where(x => x.Descricao == IntervaloEnum.InicioJornada).FirstOrDefault().Hora;
- if(modificadorFimJornada == 10){
- list.Where(x => x.Descricao == IntervaloEnum.FimJornada).FirstOrDefault().Hora = 19;
- }
- var modificadorFimIntervalo = list.Where(x => x.Descricao == IntervaloEnum.InicioIntervalo).FirstOrDefault().Hora;
- if(modificadorFimIntervalo == 13){
- list.Where(x => x.Descricao == IntervaloEnum.FimIntervalo).FirstOrDefault().Hora = 14;
- }
- return list;
- }
- }
- public class ScheduleHelper
- {
- static System.Timers.Timer timer;
- private static Intervalo _intervalo;
- public ScheduleHelper()
- {
- _intervalo = new Intervalo();
- }
- public void Run()
- {
- DateTime now = DateTime.Now;
- Console.WriteLine($"Iniciando schedule em:{now}");
- if (!(now.DayOfWeek == DayOfWeek.Sunday || now.DayOfWeek == DayOfWeek.Saturday))
- schedule_Timer_PortalRh();
- }
- static void schedule_Timer_PortalRh()
- {
- Console.WriteLine("..:: Timer Schedule Started ::..");
- DateTime nowTime = DateTime.Now;
- var intervalo = _intervalo.ListTime().Where(x => x.Data < nowTime).LastOrDefault();
- Console.WriteLine($"..:: Previsão de execução as { intervalo.Hora}:{intervalo.Minutos}:{intervalo.Segundos} ::..");
- DateTime scheduledTime = intervalo.Data;
- if (nowTime > scheduledTime)
- {
- scheduledTime = scheduledTime.AddDays(1);
- if (intervalo.Descricao == IntervaloEnum.FimJornada)
- _intervalo = new Intervalo();
- }
- double tickTime = (double)(scheduledTime - DateTime.Now).TotalMilliseconds;
- timer = new System.Timers.Timer(tickTime);
- timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_PortalRh);
- timer.Start();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- static void timer_Elapsed_PortalRh(object sender, ElapsedEventArgs e)
- {
- Console.WriteLine("..:: Timer Schedule Stopped ::..");
- var nowTime = DateTime.Now;
- timer.Stop();
- Console.WriteLine($"Iniciando processo {nowTime}");
- schedule_Timer_PortalRh();
- }
- }
- void Main()
- {
- var schedule = new ScheduleHelper();
- schedule.Run();
- Console.Read();
- }
- // Define other methods and classes here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement