private void ScheduleJob(Job job, Cron.CronStructure cs) { DateTime current = DateTime.Now; while (true) { DateTime dateTime = cs.NextOccurence.First(); Debug.WriteLine((uint)(dateTime - current).Minutes); if (scheduledJobsMapping.ContainsKey(dateTime)) { scheduledJobsMapping[dateTime].Add(job); } else { scheduledJobsMapping.Add(dateTime, new List <Job>() { job }); } if ((dateTime - current).TotalMinutes > 20) { break; } } }
private async void RunJobsAtTime(List <Job> jobs, int milisecondsDelay) { await Task.Delay(milisecondsDelay); foreach (Job job in jobs) { Debug.WriteLine("RUN JOB: " + job.Name); Cron.CronStructure cs = cronMapping[job]; ScheduleJob(job, cs); using (var context = new TaskSchedulerDbContext()) { UriAction[] uriActions = context.UriActionsForActionPredicate(x => x.JobId == job.Id); NotificationAction[] notificationActions = context.NotificationActionsForActionPredicate(x => x.JobId == job.Id); ApplicationAction[] applicationActions = context.ApplicationActionsForActionPredicate(x => x.JobId == job.Id); try { foreach (var action in uriActions) { await Launcher.LaunchUriAsync(new Uri(action.Uri)); } foreach (var action in notificationActions) { Toast.ShowToastNotification(job.Name, action.Text, action.Image, action.Audio, action.Timeout); } await appListProvider.IsDoneTask.ContinueWith(async (Task) => { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, async() => { foreach (var action in applicationActions) { var appQuery = appListProvider.AppList.Where(x => x.Package.Id.FullName == action.ApplicationName); if (appQuery.Any()) { AppEntry entry = appQuery.First(); bool opr = await entry.Entry.LaunchAsync(); while (!opr) { await Task.Delay(1000); opr = await entry.Entry.LaunchAsync(); } } } }); }); } catch (Exception e) { Debug.WriteLine(e.Message); } } } }
public void AddJob(Job job) { Cron.CronStructure cs = Cron.ParseString(job.Cron); cronMapping.Add(job, cs); ScheduleJob(job, cs); }