private static void InvokeCallbackUIThread(object sender)
 {
     UIThreadInvoker.AssertUIThread();
     try
     {
         var command = sender as DynamicMenuCommand;
         if (command == null)
         {
             throw new InvalidOperationException("invalid command");
         }
         command.ExecuteCommand();
     }
     catch (Exception ex)
     {
         ContractsVsPackage.ReportException(ex);
     }
 }
예제 #2
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initilaization code that rely on services provided by VisualStudio.
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();

            // little helper to invoke delegates in the UI thread
            UIThreadInvoker.Initialize();

            try
            {
                var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
                Contract.Assume(mcs != null);
                mcs.AddCommand(new RunCCheckInContextMenuCommand(this));
            }
            catch (Exception e)
            {
                ContractsVsPackage.ReportException(e);
            }
        }
        private static void QueryStatusCallbackUIThread(object sender)
        {
            UIThreadInvoker.AssertUIThread();
            try
            {
                DynamicMenuCommand command = sender as DynamicMenuCommand;
                if (command == null)
                {
                    throw new InvalidOperationException();
                }
                if (command.Package.Zombied)
                {
                    return;
                }

                command.QueryStatus();
            }
            catch (Exception ex)
            {
                ContractsVsPackage.ReportException(ex);
            }
        }
        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);
            }
        }
 /// <summary>
 /// Dispatches the callback to inherited method
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private static void InvokeCallback(object sender, EventArgs e)
 {
     UIThreadInvoker.Invoke(() => InvokeCallbackUIThread(sender));
 }