private void StopListening()
 {
     Logger.Info("Stopping listening on background apps activity...");
     this.watcher.Stop();
     this.watcher.Added   -= this.WatcherOnAdded;
     this.watcher.Removed -= this.WatcherOnRemoved;
     this.watcher          = null;
 }
        private void WatcherOnRemoved(AppDiagnosticInfoWatcher sender, AppDiagnosticInfoWatcherEventArgs args)
        {
            Logger.Trace($"The following app has been closed: {args.AppDiagnosticInfo.AppInfo.AppUserModelId}");
            this.syncWriterLockSlim.EnterWriteLock();

            try
            {
                var family = GetFamilyNameFromAppUserModelId(args.AppDiagnosticInfo.AppInfo.AppUserModelId);

                if (!this.activeApps.Remove(family))
                {
                    return;
                }

                Logger.Debug($"The following app family name has been closed: {args.AppDiagnosticInfo.AppInfo.AppUserModelId}");
                this.Publish(new ActivePackageFullNames(this.activeApps));
            }
            finally
            {
                this.syncWriterLockSlim.ExitWriteLock();
            }
        }
        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();
            }
        }