Index: trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs
===================================================================
--- trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs	(revision 6694)
+++ trunk/sources/HeuristicLab.Optimization/3.3/BatchRun.cs	(revision 6767)
@@ -147,5 +147,7 @@
     }
 
-    private bool stopPending;
+    private bool batchRunStarted = false;
+    private bool batchRunPaused = false;
+    private bool batchRunStopped = false;
 
     public BatchRun()
@@ -158,5 +160,4 @@
       repetitionsCounter = 0;
       Runs = new RunCollection();
-      stopPending = false;
     }
     public BatchRun(string name)
@@ -168,5 +169,4 @@
       repetitionsCounter = 0;
       Runs = new RunCollection();
-      stopPending = false;
     }
     public BatchRun(string name, string description)
@@ -177,11 +177,7 @@
       repetitionsCounter = 0;
       Runs = new RunCollection();
-      stopPending = false;
     }
     [StorableConstructor]
-    private BatchRun(bool deserializing)
-      : base(deserializing) {
-      stopPending = false;
-    }
+    private BatchRun(bool deserializing) : base(deserializing) { }
     [StorableHook(HookType.AfterDeserialization)]
     private void AfterDeserialization() {
@@ -197,5 +193,7 @@
       repetitionsCounter = original.repetitionsCounter;
       runs = cloner.Clone(original.runs);
-      stopPending = original.stopPending;
+      batchRunStarted = original.batchRunStarted;
+      batchRunPaused = original.batchRunPaused;
+      batchRunStopped = original.batchRunStopped;
       Initialize();
     }
@@ -227,19 +225,32 @@
       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
-      if (Optimizer != null) Optimizer.Start();
+      if (Optimizer == null) return;
+
+      batchRunStarted = true;
+      batchRunPaused = false;
+      batchRunStopped = false;
+      if (Optimizer.ExecutionState != ExecutionState.Prepared) Optimizer.Prepare();
+      Optimizer.Start();
     }
     public void Pause() {
       if (ExecutionState != ExecutionState.Started)
         throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
-      if ((Optimizer != null) && (Optimizer.ExecutionState == ExecutionState.Started))
-        Optimizer.Pause();
+      if (Optimizer == null) return;
+      if (Optimizer.ExecutionState != ExecutionState.Started) return;
+
+      batchRunStarted = false;
+      batchRunPaused = true;
+      batchRunStopped = false;
+      Optimizer.Pause();
     }
     public void Stop() {
       if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
-      stopPending = true;
-      if ((Optimizer != null) &&
-          ((Optimizer.ExecutionState == ExecutionState.Started) || (Optimizer.ExecutionState == ExecutionState.Paused)))
-        Optimizer.Stop();
+      if (Optimizer == null) return;
+      if (Optimizer.ExecutionState != ExecutionState.Started && Optimizer.ExecutionState != ExecutionState.Paused) return;
+      batchRunStarted = false;
+      batchRunPaused = false;
+      batchRunStopped = true;
+      Optimizer.Stop();
     }
 
@@ -324,12 +335,17 @@
     }
     private void Optimizer_Paused(object sender, EventArgs e) {
-      OnPaused();
+      if (ExecutionState == ExecutionState.Started) {
+        batchRunStarted = false;
+        batchRunPaused = true;
+        batchRunStopped = false;
+        OnPaused();
+      }
     }
     private void Optimizer_Prepared(object sender, EventArgs e) {
-      if ((ExecutionState == ExecutionState.Paused) || (ExecutionState == ExecutionState.Stopped))
+      if (ExecutionState == ExecutionState.Stopped || !batchRunStarted) {
         OnPrepared();
+      }
     }
     private void Optimizer_Started(object sender, EventArgs e) {
-      stopPending = false;
       if (ExecutionState != ExecutionState.Started)
         OnStarted();
@@ -338,10 +354,11 @@
       repetitionsCounter++;
 
-      if (!stopPending && (repetitionsCounter < repetitions)) {
+      if (batchRunStopped) OnStopped();
+      else if (repetitionsCounter >= repetitions) OnStopped();
+      else if (batchRunPaused) OnPaused();
+      else if (batchRunStarted) {
         Optimizer.Prepare();
         Optimizer.Start();
-      } else {
-        OnStopped();
-      }
+      } else OnStopped();
     }
     private void Optimizer_Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
Index: trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs
===================================================================
--- trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 6694)
+++ trunk/sources/HeuristicLab.Optimization/3.3/Experiment.cs	(revision 6767)
@@ -104,5 +104,6 @@
     }
 
-    private bool stopPending;
+    private bool experimentStarted = false;
+    private bool experimentStopped = false;
 
     public Experiment()
@@ -114,5 +115,4 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
@@ -124,5 +124,4 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
@@ -133,12 +132,8 @@
       optimizers = new OptimizerList();
       Runs = new RunCollection();
-      stopPending = false;
       Initialize();
     }
     [StorableConstructor]
-    private Experiment(bool deserializing)
-      : base(deserializing) {
-      stopPending = false;
-    }
+    private Experiment(bool deserializing) : base(deserializing) { }
     [StorableHook(HookType.AfterDeserialization)]
     private void AfterDeserialization() {
@@ -151,5 +146,7 @@
       optimizers = cloner.Clone(original.optimizers);
       runs = cloner.Clone(original.runs);
-      stopPending = original.stopPending;
+
+      experimentStarted = original.experimentStarted;
+      experimentStopped = original.experimentStopped;
       Initialize();
     }
@@ -172,35 +169,40 @@
       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused) && (ExecutionState != ExecutionState.Stopped))
         throw new InvalidOperationException(string.Format("Prepare not allowed in execution state \"{0}\".", ExecutionState));
-      if (Optimizers.Count > 0) {
-        if (clearRuns) runs.Clear();
-        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
+      if (Optimizers.Count == 0) return;
+
+      if (clearRuns) runs.Clear();
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState != ExecutionState.Started))
+        if (clearRuns || optimizer.ExecutionState != ExecutionState.Prepared)
           optimizer.Prepare(clearRuns);
-      }
     }
     public void Start() {
       if ((ExecutionState != ExecutionState.Prepared) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Start not allowed in execution state \"{0}\".", ExecutionState));
-      stopPending = false;
-      if (Optimizers.Count > 0) {
-        IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
-        if (optimizer != null) optimizer.Start();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = true;
+      experimentStopped = false;
+      IOptimizer optimizer = Optimizers.FirstOrDefault(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused));
+      if (optimizer != null) optimizer.Start();
     }
     public void Pause() {
       if (ExecutionState != ExecutionState.Started)
         throw new InvalidOperationException(string.Format("Pause not allowed in execution state \"{0}\".", ExecutionState));
-      if (Optimizers.Count > 0) {
-        foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
-          optimizer.Pause();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = false;
+      experimentStopped = false;
+      foreach (IOptimizer optimizer in Optimizers.Where(x => x.ExecutionState == ExecutionState.Started))
+        optimizer.Pause();
     }
     public void Stop() {
       if ((ExecutionState != ExecutionState.Started) && (ExecutionState != ExecutionState.Paused))
         throw new InvalidOperationException(string.Format("Stop not allowed in execution state \"{0}\".", ExecutionState));
-      stopPending = true;
-      if (Optimizers.Count > 0) {
-        foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
-          optimizer.Stop();
-      }
+      if (Optimizers.Count == 0) return;
+
+      experimentStarted = false;
+      experimentStopped = true;
+      foreach (IOptimizer optimizer in Optimizers.Where(x => (x.ExecutionState == ExecutionState.Started) || (x.ExecutionState == ExecutionState.Paused)))
+        optimizer.Stop();
     }
 
@@ -333,11 +335,9 @@
     }
     private void optimizer_Stopped(object sender, EventArgs e) {
-      if (!stopPending && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
+      if (experimentStopped && Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped || o.ExecutionState == ExecutionState.Prepared)) OnStopped();
+      else if (Optimizers.All(o => o.ExecutionState == ExecutionState.Stopped)) OnStopped();
+      else if (Optimizers.Any(o => o.ExecutionState == ExecutionState.Paused) && Optimizers.All(o => o.ExecutionState != ExecutionState.Started)) OnPaused();
+      else if (experimentStarted && !experimentStopped && Optimizers.Any(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused))) {
         Optimizers.First(x => (x.ExecutionState == ExecutionState.Prepared) || (x.ExecutionState == ExecutionState.Paused)).Start();
-      } else {
-        if (Optimizers.All(x => (x.ExecutionState != ExecutionState.Started) && (x.ExecutionState != ExecutionState.Paused))) {
-          stopPending = false;
-          OnStopped();
-        }
       }
     }
