/// <summary>All jobs have completed</summary> private void OnAllJobsCompleted(object sender, AllCompletedArgs e) { if (e.exceptionThrown != null) { errors.Add(e.exceptionThrown); } Stop(); if (errors.Count == 0) { explorerPresenter.MainPresenter.ShowMessage(jobName + " complete " + " [" + stopwatch.Elapsed.TotalSeconds.ToString("#.00") + " sec]", Simulation.ErrorLevel.Information); } else { string errorMessage = null; errors.ForEach(error => errorMessage += error.ToString() + Environment.NewLine + "----------------------------------------------" + Environment.NewLine); explorerPresenter.MainPresenter.ShowMessage(errorMessage, Simulation.ErrorLevel.Error); } SoundPlayer player = new SoundPlayer(); if (DateTime.Now.Month == 12 && DateTime.Now.Day == 25) { player.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ApsimNG.Resources.notes.wav"); } else { player.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ApsimNG.Resources.success.wav"); } player.Play(); }
/// <summary> /// Event handler for a job runner's <see cref="IJobRunner.AllJobsCompleted"/> event. /// Asserts that the job ran unsuccessfully. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="args">Event arguments.</param> private void EnsureSimulationRanRed(object sender, AllCompletedArgs args) { if (args.exceptionThrown == null) { throw new Exception(string.Format("{0} failed to throw an exception, when we expected an error to be thrown.", sender.GetType().Name)); } }
/// <summary> /// Event handler for a job runner's <see cref="IJobRunner.AllJobsCompleted"/> event. /// Asserts that the job ran successfully. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="args">Event arguments.</param> private void EnsureSimulationRanGreen(object sender, AllCompletedArgs args) { if (args.exceptionThrown != null) { throw new Exception(string.Format("Exception was thrown when running via {0}, when we expected no error to be thrown.", sender.GetType().Name), args.exceptionThrown); } }
/// <summary>Stop all jobs currently running</summary> public void Stop() { lock (this) { if (server != null) { cancelToken.Cancel(); server.StopListening(); server = null; DeleteRunners(); runningJobs.Clear(); try { jobs.Completed(); } catch (Exception err) { errors += Environment.NewLine + err.ToString(); } if (AllJobsCompleted != null) { AllCompletedArgs args = new AllCompletedArgs(); if (errors != null) { args.exceptionThrown = new Exception(errors); } AllJobsCompleted.Invoke(this, args); } } } }
/// <summary>Stop all jobs currently running</summary> public void Stop() { lock (this) { if (server != null) { cancelToken.Cancel(); server.StopListening(); server = null; DeleteRunners(); runningJobs.Clear(); jobs.Completed(); if (AllJobsCompleted != null) { AllCompletedArgs args = new AllCompletedArgs(); if (errors != null) { args.exceptionThrown = new Exception(errors); } AllJobsCompleted.Invoke(this, args); } } } }
/// <summary>All jobs have completed</summary> private static void OnAllJobsCompleted(object sender, AllCompletedArgs e) { lock (errors) { if (e.exceptionThrown != null) { errors += e.exceptionThrown.ToString() + Environment.NewLine + "----------------------------------------------" + Environment.NewLine; } } }
/// <summary> /// Generates a .csv file for each .apsimx file that has been run. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private static void GenerateCsvFiles(object sender, AllCompletedArgs e) { foreach (string file in files) { string fileName = Path.ChangeExtension(file, ".db"); Storage.IDataStore storage = new Storage.DataStore(fileName); Report.Report.WriteAllTables(storage, fileName); Console.WriteLine("Successfully created csv file " + Path.ChangeExtension(fileName, ".csv")); } }
/// <summary>All jobs have completed</summary> private void OnAllJobsCompleted(object sender, AllCompletedArgs e) { if (e.exceptionThrown != null) { errors.Add(e.exceptionThrown); } try { Stop(); } catch { // We could display the error message, but we're about to display output to the user anyway. } if (errors.Count == 0) { explorerPresenter.MainPresenter.ShowMessage(string.Format("{0} complete [{1} sec]", jobName, stopwatch.Elapsed.TotalSeconds.ToString("#.00")), Simulation.MessageType.Information); } else { explorerPresenter.MainPresenter.ShowError(errors); } SoundPlayer player = new SoundPlayer(); if (DateTime.Now.Month == 12 && DateTime.Now.Day == 25) { player.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ApsimNG.Resources.notes.wav"); } else { player.Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("ApsimNG.Resources.success.wav"); } player.Play(); Finished?.Invoke(this, EventArgs.Empty); }