Index: trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs
===================================================================
--- trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs	(revision 8190)
+++ trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs	(revision 8194)
@@ -30,4 +30,6 @@
 
 namespace HeuristicLab.Optimization {
+  internal enum BatchRunAction { None, Prepare, Start, Pause, Stop };
+
   /// <summary>
   /// A run in which an optimizer is executed a given number of times.
@@ -153,8 +155,5 @@
     }
 
-    private bool batchRunPrepared = false;
-    private bool batchRunStarted = false;
-    private bool batchRunPaused = false;
-    private bool batchRunStopped = false;
+    private BatchRunAction batchRunAction = BatchRunAction.None;
 
     public BatchRun()
@@ -204,8 +203,5 @@
       repetitionsCounter = original.repetitionsCounter;
       runs = cloner.Clone(original.runs);
-      batchRunStarted = original.batchRunStarted;
-      batchRunPaused = original.batchRunPaused;
-      batchRunStopped = original.batchRunStopped;
-      batchRunPrepared = original.batchRunPrepared;
+      batchRunAction = original.batchRunAction;
       Initialize();
     }
@@ -230,9 +226,7 @@
         repetitionsCounter = 0;
         if (clearRuns) runs.Clear();
-        batchRunPrepared = true;
-        batchRunStarted = false;
-        batchRunPaused = false;
-        batchRunStopped = false;
-        Optimizer.Prepare(clearRuns);
+        batchRunAction = BatchRunAction.Prepare;
+        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+        try { Optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
       } else {
         ExecutionState = ExecutionState.Stopped;
@@ -243,11 +237,8 @@
         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
       if (Optimizer == null) return;
-
-      batchRunPrepared = false;
-      batchRunStarted = true;
-      batchRunPaused = false;
-      batchRunStopped = false;
+      batchRunAction = BatchRunAction.Start;
       if (Optimizer.ExecutionState == ExecutionState.Stopped) Optimizer.Prepare();
-      Optimizer.Start();
+      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+      try { Optimizer.Start(); } catch (InvalidOperationException) { }
     }
     public void Pause() {
@@ -255,11 +246,8 @@
         throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
       if (Optimizer == null) return;
+      batchRunAction = BatchRunAction.Pause;
       if (Optimizer.ExecutionState != ExecutionState.Started) return;
-
-      batchRunPrepared = false;
-      batchRunStarted = false;
-      batchRunPaused = true;
-      batchRunStopped = false;
-      Optimizer.Pause();
+      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+      try { Optimizer.Pause(); } catch (InvalidOperationException) { }
     }
     public void Stop() {
@@ -267,14 +255,11 @@
         throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
       if (Optimizer == null) return;
+      batchRunAction = BatchRunAction.Stop;
       if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) {
         OnStopped();
         return;
       }
-
-      batchRunPrepared = false;
-      batchRunStarted = false;
-      batchRunPaused = false;
-      batchRunStopped = true;
-      Optimizer.Stop();
+      // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+      try { Optimizer.Stop(); } catch (InvalidOperationException) { }
     }
 
@@ -302,5 +287,5 @@
     public event EventHandler Prepared;
     private void OnPrepared() {
-      batchRunPrepared = false;
+      batchRunAction = BatchRunAction.None;
       ExecutionState = ExecutionState.Prepared;
       EventHandler handler = Prepared;
@@ -309,5 +294,5 @@
     public event EventHandler Started;
     private void OnStarted() {
-      //TODO add coment
+      // no reset of BatchRunAction.Started, because we need to differ which of the two was started by the user
       ExecutionState = ExecutionState.Started;
       EventHandler handler = Started;
@@ -316,5 +301,5 @@
     public event EventHandler Paused;
     private void OnPaused() {
-      batchRunPaused = false;
+      batchRunAction = BatchRunAction.None;
       ExecutionState = ExecutionState.Paused;
       EventHandler handler = Paused;
@@ -323,6 +308,5 @@
     public event EventHandler Stopped;
     private void OnStopped() {
-      //reset flags because it cannot be done if the optimizer gets prepared
-      batchRunStopped = false;
+      batchRunAction = BatchRunAction.None;
       ExecutionState = ExecutionState.Stopped;
       EventHandler handler = Stopped;
@@ -369,5 +353,5 @@
     }
     private void Optimizer_Prepared(object sender, EventArgs e) {
-      if (batchRunPrepared || (ExecutionState == ExecutionState.Stopped)) {
+      if (batchRunAction == BatchRunAction.Prepare || ExecutionState == ExecutionState.Stopped) {
         ExecutionTime = TimeSpan.Zero;
         runsExecutionTime = TimeSpan.Zero;
@@ -385,8 +369,8 @@
       runsExecutionTime = TimeSpan.Zero;
 
-      if (batchRunStopped) OnStopped();
+      if (batchRunAction == BatchRunAction.Stop) OnStopped();
       else if (repetitionsCounter >= repetitions) OnStopped();
-      else if (batchRunPaused) OnPaused();
-      else if (batchRunStarted) {
+      else if (batchRunAction == BatchRunAction.Pause) OnPaused();
+      else if (batchRunAction == BatchRunAction.Start) {
         Optimizer.Prepare();
         Optimizer.Start();
Index: trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs
===================================================================
--- trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 8190)
+++ trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 8194)
@@ -178,7 +178,8 @@
       experimentStarted = false;
       experimentStopped = false;
-      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
-        //if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
-        optimizer.Prepare(clearRuns);
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started)) {
+        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+        try { optimizer.Prepare(clearRuns); } catch (InvalidOperationException) { }
+      }
     }
     public void Start() {
@@ -190,5 +191,8 @@
       experimentStopped = false;
       IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
-      if (optimizer != null) optimizer.Start();
+      if (optimizer != null) {
+        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+        try { optimizer.Start(); } catch (InvalidOperationException) { }
+      }
     }
     public void Pause() {
@@ -199,6 +203,8 @@
       experimentStarted = false;
       experimentStopped = false;
-      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
-        optimizer.Pause();
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started)) {
+        // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+        try { optimizer.Pause(); } catch (InvalidOperationException) { }
+      }
     }
     public void Stop() {
@@ -210,6 +216,8 @@
       experimentStopped = true;
       if (Optimizers.Any(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
-        foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
-          optimizer.Stop();
+        foreach (var optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused))) {
+          // a race-condition may occur when the optimizer has changed the state by itself in the meantime
+          try { optimizer.Stop(); } catch (InvalidOperationException) { }
+        }
       } else {
         OnStopped();
