Exemplo n.º 1
0
        public override bool Execute()
        {
            if (string.IsNullOrEmpty(LaunchProfiles) ||
                string.IsNullOrEmpty(UserFile))
            {
                Debug.Fail("Should have gotten something to monitor");
                return(true);
            }

            try
            {
                if (BuildEngine4.GetRegisteredTaskObject(nameof(ActiveDocumentMonitor), RegisteredTaskObjectLifetime.AppDomain) is not ActiveDocumentMonitor monitor)
                {
                    var maxAttempts = 5;
                    for (var i = 1; i <= maxAttempts; i++)
                    {
                        if (WindowsInterop.GetServiceProvider() is IServiceProvider services)
                        {
                            BuildEngine4.RegisterTaskObject(nameof(ActiveDocumentMonitor),
                                                            new ActiveDocumentMonitor(LaunchProfiles !, UserFile !,
                                                                                      StartupFiles.Select(x => x.ItemSpec).ToArray(), services),
                                                            RegisteredTaskObjectLifetime.AppDomain, false);

                            return(true);
                        }
                        else
                        {
                            try
                            {
                                BuildEngine4.Yield();
                            }
                            catch (Exception e)
                            {
                                Debug.WriteLine(e);
                            }
                            // Increase the wait time to allow more time between retries
                            Thread.Sleep(200 * i);
                        }
                    }

                    Debug.Fail("Failed to get IServiceProvider to monitor for active document.");
                }
                else
                {
                    // NOTE: this means we only support ONE project/launchProfiles per IDE.
                    monitor.Refresh(LaunchProfiles !, UserFile !,
                                    StartupFiles.Select(x => x.ItemSpec).ToArray());
                }
            }