コード例 #1
0
        public IList <string> GetCurrentlyRunningPackageNames()
        {
            Logger.Info("Getting the list of running apps...");

            try
            {
                Logger.Trace("Entering upgradeable read lock...");
                this.syncWriterLockSlim.EnterUpgradeableReadLock();

                if (this.watcher == null)
                {
                    Logger.Info("Starting listening on background apps activity...");
                    this.watcher = AppDiagnosticInfo.CreateWatcher();
                    this.watcher.Start();
                }
                else
                {
                    this.watcher.Removed -= this.WatcherOnRemoved;
                    this.watcher.Added   -= this.WatcherOnAdded;
                }

                if (this.activeApps != null)
                {
                    return(this.activeApps.ToList());
                }

                Logger.Trace("Upgrading to write lock...");
                this.syncWriterLockSlim.EnterWriteLock();

                try
                {
                    this.activeApps = new HashSet <string>(StringComparer.Ordinal);

                    foreach (var item in AppDiagnosticInfo.RequestInfoAsync().GetAwaiter().GetResult())
                    {
                        var family = GetFamilyNameFromAppUserModelId(item.AppInfo.AppUserModelId);
                        this.activeApps.Add(family);
                    }

                    Logger.Info($"Returning {this.activeApps.Count} apps running in the background...");
                }
                finally
                {
                    this.syncWriterLockSlim.ExitWriteLock();
                }

                this.Publish(new ActivePackageFullNames(this.activeApps));
                return(this.activeApps.ToList());
            }
            finally
            {
                if (this.watcher != null)
                {
                    this.watcher.Removed += this.WatcherOnRemoved;
                    this.watcher.Added   += this.WatcherOnAdded;
                }

                Logger.Trace("Exiting upgradeable read lock...");
                this.syncWriterLockSlim.ExitUpgradeableReadLock();
            }
        }