/// <summary> /// Write in the settings file the changes in the app settings. /// </summary> /// <param name="app">App controller.</param> public void UpdateSettings(AppController app) { //Se o app for nulo, lança um erro if (app == null) { throw new ArgumentNullException(nameof(app), "The inserted application is invalid"); } //Se a configuração não existir, lança um erro if (!this.ExistSettings(app.Id)) { throw new FileNotFoundException("The application configuration was not found."); } //Valida a canfiguração this.ValidateSettings(app.Settings, true, app.Id); //Salva a configuração em um arquivo this.WriteSettings(app.Id, app.Settings); //Chama o event handler de "configuração atualizada" this.OnConfigChanged?.Invoke(this, new AppEventArgs(app)); }
/// <summary> /// Starts the task responsible for starting and closing apps according to your settings. /// </summary> /// <param name="schedulerDelay">Delay para atualizar os apps.</param> /// <param name="autoReload">Automatically recovers new apps added to the StartupController.AppsDir folder.</param> /// <param name="runInBackground">Signals whether the thread should run in the background.</param> /// <param name="closeAfterWorktime">Signals whether the application should be automatically terminated by the task when the working time comes to an end and AppController.ForceCloseAfterWorktime = true.</param> public void RunScheduler(TimeSpan schedulerDelay, bool autoReload, bool runInBackground, bool closeAfterWorktime = true) { if (this.schedulerThread != null) { throw new InvalidOperationException("The scheduler thread has already started."); } void thStart() { while (true) { AppController lastApp = null; try { if (autoReload) { this.ReloadApps(); } foreach (var app in this.Apps) { //Define qual é o app que está sendo processado lastApp = app; //Se as configurações do app estiverem desativadas ou se o app estiver com erro: //Ignorar os próximos passos if (!app.Enabled || app.State == AppState.Errored) { continue; } try { //Se o app não estiver funcionando, TryAttach() = false e InWorkingTime = true: //Inicia o app if (!app.IsRunning && !app.TryAttach() && app.InWorkingTime) { app.Start(); } } catch (SystemException ex) { this.OnError.Invoke(this, new AppErrorEventArgs(lastApp, ex, true)); continue; } //Se o controlador não tem permissão para encerrar os apps depois do tempo limite //Ou o aplicativo não está aberto ou o aplicativo está no tempo de trabalho //Ou o aplicativo foi iniciado pelo usuário //Ou o aplicativo não deve ser encerrado após o tempo de trabalho //Ou o aplicativo não tem um horário de início definido //Ou o aplicativo foi iniciado após o tempo limite: //Ignorar o próximo passo if (!closeAfterWorktime || !app.IsRunning || app.InWorkingTime || app.StartedByUser || !app.Settings.ForceCloseAfterWorktime || app.StartTime == null || app.StartedOutOfWorktime) { continue; } var args = new AppEndWorkEventArgs(app, false); this.OnEndWork?.Invoke(this, args); if (args.IsCancelled) { continue; } app.Close(); } Thread.Sleep(schedulerDelay); } catch (ThreadInterruptedException) { break; } catch (Exception ex) { this.OnError.Invoke(this, new AppErrorEventArgs(lastApp, ex, true)); break; } } } this.schedulerThread = new Thread(thStart) { IsBackground = runInBackground }; this.schedulerThread.Start(); }