protected override void Execute() { if (VsShellUtilities.IsSolutionBuilding(this.Package)) { return; } try { UIThreadInvoker.Invoke( // Use it to force the execution in the UI thread delegate { var EnvVars = new Dictionary <string, string>(); Project project; string projectGuid; if (!ProjectHelper.TryGetActiveProject(this.Package, out project) || !ProjectHelper.TryGetProjectGuid(this.Package, project, out projectGuid)) { MessageBox.Show( "Code Contracts could not detect the current project", "Code Contracts: Could not detect the current project", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } EnvVars["CodeContractsTargetProjectGuid"] = projectGuid; string name; if (ContextHelper.TryGetSelectedMemberFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetMember"] = name; } else if (ContextHelper.TryGetSelectedTypeFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetType"] = name; } else if (ContextHelper.TryGetSelectedNamespaceFullNameMangled(this.Package, out name)) { EnvVars["CodeContractsTargetNamespace"] = name; } else { MessageBox.Show( "Code Contracts could not detect the selected type, member or namespace. " + "Please make sure that the editor caret is inside the member to test.", "Code Contracts: Could not detect the code context", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } ThreadPool.QueueUserWorkItem(o => { try { foreach (var p in EnvVars) { Environment.SetEnvironmentVariable(p.Key, p.Value); } ProjectHelper.TryBuildProject(this.Package, project); } catch (Exception e) { ContractsVsPackage.ReportException(e); } finally { foreach (var p in EnvVars) { Environment.SetEnvironmentVariable(p.Key, null); } } }); }); } catch (Exception ex) { ContractsVsPackage.ReportException(ex); } }