From 22a4842e9e3c8716d138552c937d0df7c6fd2dc1 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Tue, 4 Oct 2005 19:35:26 +0000 Subject: [PATCH] Run MsBuild in a separate thread => you can continue coding while your code is compiling. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@538 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Base/Project/Src/Project/MSBuildEngine.cs | 101 +++++++++++------- .../StartUp/Project/Dialogs/ExceptionBox.cs | 9 +- 2 files changed, 68 insertions(+), 42 deletions(-) diff --git a/src/Main/Base/Project/Src/Project/MSBuildEngine.cs b/src/Main/Base/Project/Src/Project/MSBuildEngine.cs index 00bfa423a9..fa620c3cb2 100644 --- a/src/Main/Base/Project/Src/Project/MSBuildEngine.cs +++ b/src/Main/Base/Project/Src/Project/MSBuildEngine.cs @@ -56,66 +56,87 @@ namespace ICSharpCode.SharpDevelop.Project return Run(buildFile, null); } + static bool IsRunning = false; + public CompilerResults Run(string buildFile, string[] targets) { - LoggingService.Debug("Run MSBuild on " + buildFile); - - // HACK: Workaround for MSBuild bug: - // "unknown" MSBuild projects (projects with unknown type id, e.g. IL-projects) - // are looked up by MSBuild if the project files are MSBuild-compatible. - // That lookup method has a bug: it uses the working directory instead of the - // solution directory as base path for the lookup. - Environment.CurrentDirectory = Path.GetDirectoryName(buildFile); - CompilerResults results = new CompilerResults(null); - BuildPropertyGroup properties = new BuildPropertyGroup(); - string location = Path.GetDirectoryName(typeof(MSBuildEngine).Assembly.Location); - properties.SetProperty("SharpDevelopBinPath", location); - foreach (KeyValuePair entry in MsBuildProperties) { - properties.SetProperty(entry.Key, entry.Value); + if (IsRunning) { + results.Errors.Add(new CompilerError(null, 0, 0, null, "MsBuild is already running!")); + return results; + } + IsRunning = true; + try { + Thread thread = new Thread(new ThreadStarter(results, buildFile, targets, this).Run); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + while (!thread.Join(10)) { + Application.DoEvents(); + Application.DoEvents(); + } + return results; + } finally { + IsRunning = false; } + } + + class ThreadStarter + { + CompilerResults results; + string buildFile; + string[] targets; + MSBuildEngine engine; - Engine engine = new Engine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()); - SharpDevelopLogger logger = new SharpDevelopLogger(this, results); - engine.RegisterLogger(logger); - engine.BuildProjectFile(buildFile, targets, properties, null); - logger.FlushText(); + public ThreadStarter(CompilerResults results, string buildFile, string[] targets, MSBuildEngine engine) + { + this.results = results; + this.buildFile = buildFile; + this.targets = targets; + this.engine = engine; + } - LoggingService.Debug("MSBuild finished"); - return results; + [STAThread] + public void Run() + { + LoggingService.Debug("Run MSBuild on " + buildFile); + + // HACK: Workaround for MSBuild bug: + // "unknown" MSBuild projects (projects with unknown type id, e.g. IL-projects) + // are looked up by MSBuild if the project files are MSBuild-compatible. + // That lookup method has a bug: it uses the working directory instead of the + // solution directory as base path for the lookup. + Environment.CurrentDirectory = Path.GetDirectoryName(buildFile); + + BuildPropertyGroup properties = new BuildPropertyGroup(); + string location = Path.GetDirectoryName(typeof(MSBuildEngine).Assembly.Location); + properties.SetProperty("SharpDevelopBinPath", location); + foreach (KeyValuePair entry in MsBuildProperties) { + properties.SetProperty(entry.Key, entry.Value); + } + + Engine engine = new Engine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()); + SharpDevelopLogger logger = new SharpDevelopLogger(this.engine, results); + engine.RegisterLogger(logger); + engine.BuildProjectFile(buildFile, targets, properties, null); + + LoggingService.Debug("MSBuild finished"); + } } class SharpDevelopLogger : ILogger { MSBuildEngine engine; CompilerResults results; - Thread mainThread; public SharpDevelopLogger(MSBuildEngine engine, CompilerResults results) { - mainThread = Thread.CurrentThread; this.engine = engine; this.results = results; } - StringBuilder textToWrite = new StringBuilder(); - void AppendText(string text) { - lock (textToWrite) { - textToWrite.AppendLine(text); - if (Thread.CurrentThread == mainThread) { - FlushText(); - } - } - } - - public void FlushText() - { - if (engine.MessageView != null) { - engine.MessageView.AppendText(textToWrite.ToString()); - } - textToWrite.Length = 0; + engine.MessageView.AppendText(text + "\r\n"); } void OnBuildStarted(object sender, BuildStartedEventArgs e) @@ -140,7 +161,6 @@ namespace ICSharpCode.SharpDevelop.Project { projectFiles.Push(e.ProjectFile); StatusBarService.SetMessage("Building " + Path.GetFileNameWithoutExtension(e.ProjectFile) + "..."); - Application.DoEvents(); } void OnProjectFinished(object sender, ProjectFinishedEventArgs e) @@ -148,7 +168,6 @@ namespace ICSharpCode.SharpDevelop.Project projectFiles.Pop(); if (projectFiles.Count > 0) { StatusBarService.SetMessage("Building " + Path.GetFileNameWithoutExtension(projectFiles.Peek()) + "..."); - Application.DoEvents(); } } diff --git a/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs b/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs index 059f733af3..bf4f86ea1c 100644 --- a/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs +++ b/src/Main/StartUp/Project/Dialogs/ExceptionBox.cs @@ -55,7 +55,14 @@ namespace ICSharpCode.SharpDevelop string str = ""; str += ".NET Version : " + Environment.Version.ToString() + Environment.NewLine; str += "OS Version : " + Environment.OSVersion.ToString() + Environment.NewLine; - str += "Boot Mode : " + SystemInformation.BootMode + Environment.NewLine; + try { + if (SystemInformation.TerminalServerSession) { + str += "Terminal Server Session" + Environment.NewLine; + } + if (SystemInformation.BootMode != BootMode.Normal) { + str += "Boot Mode : " + SystemInformation.BootMode + Environment.NewLine; + } + } catch {} str += "Working Set Memory : " + (Environment.WorkingSet / 1024) + "kb" + Environment.NewLine; Version v = Assembly.GetEntryAssembly().GetName().Version; str += "SharpDevelop Version : " + v.Major + "." + v.Minor + "." + v.Build + "." + v.Revision + Environment.NewLine;