Пример #1
0
        private void CompileAllCommandCallback(object sender, EventArgs args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            Interlocked.Exchange(ref package.IsCompiling, 1);

            var projs    = new List <Project>();
            var selitems = package.dte2.ToolWindows.SolutionExplorer.UIHierarchyItems;

            if (selitems != null)
            {
                foreach (UIHierarchyItem uiitem in selitems)
                {
                    if (uiitem != null)
                    {
                        var proj = uiitem.Object as Project;
                        if (proj != null)
                        {
                            projs.Add(proj);
                        }
                    }
                }
            }

            UPPCompiler.RaiseReported(null, new CompilerReportedEventArgs(null));

            package.JoinableTaskFactory.RunAsyncAsVsTask(VsTaskRunContext.UIThreadBackgroundPriority,
                                                         async cancellationToken =>
            {
                await package.JoinableTaskFactory.SwitchToMainThreadAsync();

                IVsStatusbar statusBar = (IVsStatusbar)package.GetServiceAsync(typeof(SVsStatusbar));

                object icon = (short)Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_Build;

                statusBar?.Animation(1, ref icon);

                var tasks = package.Compile(projs);

                await TaskScheduler.Default;

                await Task.WhenAll(tasks.Select(vtask => vtask.WaitForFinishedAsync()));

                await package.JoinableTaskFactory.SwitchToMainThreadAsync();

                statusBar?.Animation(0, ref icon);

                Interlocked.Exchange(ref package.IsCompiling, 0);

                return(VSConstants.S_OK);
            });
        }
Пример #2
0
        private void CompileCommandCallback(object sender, EventArgs args)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            //var mainWindow = System.Windows.Application.Current.MainWindow;
            //var contentControl = mainWindow?.Template?.FindName("PART_SccStatusBarHost", mainWindow) as System.Windows.Controls.ContentControl;

            //var statusbar = contentControl?.Content as System.Windows.Controls.Primitives.StatusBar;

            //statusbar.Items.Add(new System.Windows.Controls.Button() { Content = "U++test" });

            Interlocked.Exchange(ref package.IsCompiling, 1);

            if (package.dte2.Solution.SolutionBuild.BuildState != vsBuildState.vsBuildStateInProgress)
            {
                var projs    = new List <Project>();
                var selitems = package.dte2.ToolWindows.SolutionExplorer.SelectedItems as Array;
                if (selitems != null)
                {
                    foreach (var selitem in selitems)
                    {
                        var uiitem = selitem as UIHierarchyItem;
                        if (uiitem != null)
                        {
                            var proj = uiitem.Object as Project;
                            if (proj != null)
                            {
                                projs.Add(proj);
                            }
                        }
                    }
                }

                UPPCompiler.RaiseReported(null, new CompilerReportedEventArgs(null));

                package.JoinableTaskFactory.RunAsyncAsVsTask(VsTaskRunContext.UIThreadBackgroundPriority,
                                                             async cancellationToken =>
                {
                    await package.JoinableTaskFactory.SwitchToMainThreadAsync();

                    IVsMonitorSelection monitorSelection = await package.GetServiceAsync(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
                    Assumes.Present(monitorSelection);
                    var sol_build_guid    = VSConstants.UICONTEXT_SolutionBuilding;
                    uint sol_build_cookie = 0;
                    monitorSelection.GetCmdUIContextCookie(ref sol_build_guid, out sol_build_cookie);

                    int uiactive = -1;
                    monitorSelection.IsCmdUIContextActive(sol_build_cookie, out uiactive);
                    if (uiactive == 0)
                    {
                        monitorSelection.SetCmdUIContext(sol_build_cookie, 1);
                    }


                    IVsStatusbar statusBar = await package.GetServiceAsync(typeof(SVsStatusbar)) as IVsStatusbar;

                    object icon = (short)Microsoft.VisualStudio.Shell.Interop.Constants.SBAI_Build;

                    statusBar?.SetText("开始编译...");
                    statusBar?.Animation(1, ref icon);

                    var tasks = package.Compile(projs);

                    statusBar?.SetText("编译...");

                    await TaskScheduler.Default;

                    await Task.WhenAll(tasks.Select(vtask => vtask.WaitForFinishedAsync()));

                    await package.JoinableTaskFactory.SwitchToMainThreadAsync();

                    statusBar?.SetText("链接源文件...");
                    tasks.ForEach(task => task.ReLink());

                    statusBar?.Animation(0, ref icon);

                    Interlocked.Exchange(ref package.IsCompiling, 0);

                    uiactive = -1;
                    monitorSelection.IsCmdUIContextActive(sol_build_cookie, out uiactive);
                    if (uiactive == 1)
                    {
                        monitorSelection.SetCmdUIContext(sol_build_cookie, 0);
                    }

                    statusBar?.SetText("已完成");
                    statusBar?.Clear();

                    return(VSConstants.S_OK);
                });
            }
        }