diff --git a/src/Main/Base/Project/Src/Project/BuildEngine.cs b/src/Main/Base/Project/Src/Project/BuildEngine.cs index d6a9acc4b1..1b47cea484 100644 --- a/src/Main/Base/Project/Src/Project/BuildEngine.cs +++ b/src/Main/Base/Project/Src/Project/BuildEngine.cs @@ -106,8 +106,8 @@ namespace ICSharpCode.SharpDevelop.Project { EventHandler eh = null; lock (lockObject) { - if (isCancelAllowed) { - ICSharpCode.Core.LoggingService.Debug(""); + if (isCancelAllowed && !isCancelled) { + ICSharpCode.Core.LoggingService.Debug("Cancel build"); isCancelled = true; eh = Cancelled; } @@ -493,9 +493,11 @@ namespace ICSharpCode.SharpDevelop.Project return; buildIsDone = true; } - foreach (BuildNode n in nodeDict.Values) { - if (!n.buildFinished) { - throw new Exception("All workers done, but a project did not finish building"); + if (!buildIsCancelled) { + foreach (BuildNode n in nodeDict.Values) { + if (!n.buildFinished) { + throw new Exception("All workers done, but a project did not finish building"); + } } } string buildTime = " (" + (DateTime.Now - buildStart).ToString() + ")"; diff --git a/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs b/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs index 1920c346fe..a9840f0649 100644 --- a/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs +++ b/src/Main/Base/Project/Src/Services/ProjectService/ProjectService.cs @@ -460,6 +460,11 @@ namespace ICSharpCode.SharpDevelop.Project public static void CloseSolution() { + // If a build is running, cancel it. + // If we would let a build run but unload the MSBuild projects, the next project.StartBuild call + // could cause an exception. + BuildEngine.CancelGuiBuild(); + if (openSolution != null) { CurrentProject = null; OnSolutionClosing(new SolutionEventArgs(openSolution));