Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit cdc32915 authored by Michael KRISPER's avatar Michael KRISPER
Browse files

removed backgroundworker

parent 84350f4b
No related branches found
No related tags found
No related merge requests found
......@@ -42,7 +42,12 @@ namespace TUGraz.VectoCore.Models.Simulation
/// <summary>
/// Run the simulation.
/// </summary>
void Run(BackgroundWorker worker = null, Action<double> reportProgressAction = null);
void Run();
/// <summary>
/// Cancel the running simulation.
/// </summary>
void Cancel();
/// <summary>
/// unique identifier of a single run
......@@ -54,6 +59,8 @@ namespace TUGraz.VectoCore.Models.Simulation
string RunSuffix { get; }
double Progress { get; }
/// <summary>
/// Return the vehicle container.
/// </summary>
......
......@@ -31,10 +31,10 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using TUGraz.VectoCommon.Models;
using TUGraz.VectoCore.Models.SimulationComponent.Data;
using TUGraz.VectoCore.OutputData;
......@@ -74,7 +74,10 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
public IEnumerable<CycleTypeDescription> GetCycleTypes()
{
return Runs.Select(r => new CycleTypeDescription {Name = r.Run.CycleName,CycleType = r.Run.GetContainer().RunData.Cycle.CycleType}).Distinct();
return
Runs.Select(
r => new CycleTypeDescription { Name = r.Run.CycleName, CycleType = r.Run.GetContainer().RunData.Cycle.CycleType })
.Distinct();
}
/// <summary>
......@@ -84,7 +87,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
public List<int> AddRuns(SimulatorFactory factory)
{
var runIDs = new List<int>();
factory.SumData = _sumWriter;
factory.JobNumber = Interlocked.Increment(ref _jobNumber);
......@@ -137,7 +140,6 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
ResetEvent.WaitOne();
}
private void JobCompleted()
{
var next = Runs.FirstOrDefault(x => x.Started == false);
......@@ -153,17 +155,19 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
public Dictionary<int, ProgressEntry> GetProgress()
{
return Runs.ToDictionary(jobEntry => jobEntry.Run.RunIdentifier, entry => new ProgressEntry {
RunName = entry.Run.RunName,
CycleName = entry.Run.CycleName,
RunSuffix = entry.Run.RunSuffix,
Progress = entry.Progress,
Done = entry.Done,
ExecTime = entry.ExecTime,
Success = entry.Success,
Canceled = entry.Canceled,
Error = entry.ExecException
});
return Runs.ToDictionary(
r => r.Run.RunIdentifier,
r => new ProgressEntry {
RunName = r.Run.RunName,
CycleName = r.Run.CycleName,
RunSuffix = r.Run.RunSuffix,
Progress = r.Run.Progress,
Done = r.Done,
ExecTime = r.ExecTime,
Success = r.Success,
Canceled = r.Canceled,
Error = r.ExecException
});
}
public bool AllCompleted
......@@ -184,7 +188,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
public string RunSuffix;
}
internal class RunEntry : LoggingObject, IDisposable
internal class RunEntry : LoggingObject
{
public IVectoRun Run;
public JobContainer JobContainer;
......@@ -196,55 +200,27 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
public double ExecTime;
public Exception ExecException;
private readonly BackgroundWorker _worker = new BackgroundWorker();
public RunEntry()
{
_worker.DoWork += OnDoWork;
_worker.RunWorkerCompleted += OnRunWorkerCompleted;
_worker.WorkerSupportsCancellation = true;
}
public void RunWorkerAsync()
{
_worker.RunWorkerAsync();
Task.Run(() => {
var stopWatch = Stopwatch.StartNew();
try {
Run.Run();
} catch (Exception ex) {
Log.Error(ex, "Error during simulation run!");
ExecException = ex;
}
stopWatch.Stop();
Success = Run.FinishedWithoutErrors;
Done = true;
ExecTime = stopWatch.Elapsed.TotalMilliseconds;
JobContainer.JobCompleted();
});
}
public void CancelAsync()
{
_worker.CancelAsync();
}
private void OnDoWork(object sender, DoWorkEventArgs e)
{
var stopWatch = Stopwatch.StartNew();
try {
Run.Run(_worker, x => Progress = x);
} catch (Exception ex) {
Log.Error(ex, "Error during simulation run!");
ExecException = ex;
}
if (_worker.CancellationPending) {
e.Cancel = true;
Canceled = true;
}
stopWatch.Stop();
Success = Run.FinishedWithoutErrors;
Done = true;
ExecTime = stopWatch.Elapsed.TotalMilliseconds;
JobContainer.JobCompleted();
}
private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null) {
ExecException = e.Error;
}
}
public void Dispose()
{
_worker.Dispose();
Run.Cancel();
}
}
}
......
......@@ -30,7 +30,6 @@
*/
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Threading;
using TUGraz.VectoCommon.Exceptions;
......@@ -53,6 +52,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
protected Second AbsTime = 0.SI<Second>();
// ReSharper disable once InconsistentNaming
protected Second dt = 1.SI<Second>();
private bool _cancelled;
protected SummaryDataContainer SumWriter { get; set; }
protected string JobName { get; set; }
protected ISimulationOutPort CyclePort { get; set; }
......@@ -61,6 +61,7 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
protected IVehicleContainer Container { get; set; }
public bool FinishedWithoutErrors { get; protected set; }
public int RunIdentifier { get; protected set; }
public string RunName
......@@ -78,6 +79,11 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
get { return Container.RunData.ModFileSuffix; }
}
public double Progress
{
get { return CyclePort.Progress; }
}
protected VectoRun(IVehicleContainer container)
{
Container = container;
......@@ -91,13 +97,14 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
return Container;
}
public void Run(BackgroundWorker worker = null, Action<double> reportProgressAction = null)
public void Run()
{
var debug = new DebugData();
Log.Info("VectoJob started running.");
//var lastProgress = -1.0;
Initialize();
try {
IResponse response;
......@@ -107,11 +114,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
if (response is ResponseSuccess) {
Container.CommitSimulationStep(AbsTime, dt);
AbsTime += dt;
if (reportProgressAction != null) {
reportProgressAction(CyclePort.Progress);
}
if (worker != null && worker.CancellationPending) {
Log.Error("Background Task canceled!");
if (_cancelled) {
Log.Error("Run canceled!");
Container.RunStatus = Status.Canceled;
Container.FinishSimulation();
return;
......@@ -145,12 +149,16 @@ namespace TUGraz.VectoCore.Models.Simulation.Impl
}
Container.RunStatus = Status.Success;
Container.FinishSimulation();
IterationStatistics.FinishSimulation(RunName + CycleName + RunSuffix + RunIdentifier);
Log.Info("VectoJob finished.");
}
public void Cancel()
{
_cancelled = true;
}
private static object TryCatch(Func<object> action)
{
try {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment