public Task Register(string taskName, string taskEntryPoint, DateTime startDateTime, TimeKind tKind, uint interval) { if (IsRegistered(taskName)) { return(Task.CompletedTask); } try { DateTime nowNoSecond = DateTime.ParseExact(DateTime.Now.ToString("dd-MM-yyyy HH:mm:00"), "dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); uint initialDelay = (uint)Math.Round((startDateTime - nowNoSecond).TotalMinutes, 0); uint timeInterval = interval; Java.Util.Concurrent.TimeUnit unit = Java.Util.Concurrent.TimeUnit.Minutes; switch (tKind) { case TimeKind.Minute: unit = Java.Util.Concurrent.TimeUnit.Minutes; break; case TimeKind.Hour: unit = Java.Util.Concurrent.TimeUnit.Hours; break; case TimeKind.Day: unit = Java.Util.Concurrent.TimeUnit.Days; break; case TimeKind.Month: unit = Java.Util.Concurrent.TimeUnit.Days; timeInterval *= 30; break; case TimeKind.Year: unit = Java.Util.Concurrent.TimeUnit.Days; timeInterval *= 365; break; default: throw new Exception(string.Format("Categoria di tempo {0} non prevista", tKind)); } PeriodicWorkRequest.Builder workRequestedBuilder; if (taskName.CompareTo(App.CheckPasswordBackgroundTaskName) == 0) { workRequestedBuilder = PeriodicWorkRequest.Builder.From <CheckPasswordWorker>(timeInterval, unit); } else if (taskName.CompareTo(App.BackupBackgroundTaskName) == 0) { workRequestedBuilder = PeriodicWorkRequest.Builder.From <BackupWorker>(timeInterval, unit); } else { throw new Exception("Task not found"); } Data data = new Data.Builder() .PutLong("interval", unit.ToMinutes(timeInterval)) .Build(); PeriodicWorkRequest workRequested = (PeriodicWorkRequest)workRequestedBuilder .AddTag(taskName) .SetInitialDelay(initialDelay, Java.Util.Concurrent.TimeUnit.Minutes) .SetInputData(data) .Build(); AppWorkManager.EnqueueUniquePeriodicWork(taskName, ExistingPeriodicWorkPolicy.Replace, workRequested); } catch (Exception e) { Log.Error(Tag, string.Format("Error occurred during scheduling the work with name \"{0}\": {1}", taskName, e.Message)); } return(Task.CompletedTask); }
public async Task Register(string taskName, string taskEntryPoint, DateTime startDateTime, TimeKind tKind, uint interval) { if (IsRegistered(taskName)) { return; } try { if (startDateTime < DateTime.Now) { throw new Exception("Data di partenza anteriore alla data odierna"); } var settings = Windows.Storage.ApplicationData.Current.LocalSettings; BackgroundExecutionManager.RemoveAccess(); await BackgroundExecutionManager.RequestAccessAsync(); var builder = new BackgroundTaskBuilder { Name = taskName, TaskEntryPoint = taskEntryPoint }; builder.SetTrigger(new TimeTrigger(MinTimePeriod, false)); // ATTENZIONE: meno di 15 minuti non si può fare // Calcola il primo intervallo per partire alla data indicata DateTime nowNoSeconds = DateTime.ParseExact(DateTime.Now.ToString("dd-MM-yyyy HH:mm:00"), "dd-MM-yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); uint timeIntervalFirst = (uint)Math.Round((startDateTime - nowNoSeconds).TotalMinutes, 0); BackgroundTaskRegistration task = builder.Register(); settings.Values[taskName + ".Interval"] = timeIntervalFirst; settings.Values[taskName + ".TimeKind"] = (int)tKind; settings.Values[taskName + ".TimeQty"] = interval; settings.Values[taskName + ".NextDate"] = nowNoSeconds.AddMinutes(timeIntervalFirst).ToString("dd-MM-yyyy, HH:mm:ss"); Debug.WriteLine(string.Format("Registrata l'attività {0} con intervallo {1} {2}, a partire dal {3}", taskName, interval, tKind, startDateTime.ToString("dd/MM/yyyy HH:mm"))); } catch (Exception ex) { Debug.WriteLine(string.Format("Errore durante la registrazione del task {0}: {1}", taskName, ex.Message)); Unregister(taskName); } }