Ejemplo n.º 1
0
        private async void Page_LoadedAsync(object sender, RoutedEventArgs e)
        {
            foreach (var info in await AppDiagnosticInfo.RequestInfoAsync())
            {
                /*Items.Add(new AppInfo
                 * {
                 *  Id = info.AppInfo.Id,
                 *  PackageFamilyName = info.AppInfo.PackageFamilyName,
                 *  Description = info.AppInfo.DisplayInfo.Description,
                 *  DisplayName = info.AppInfo.DisplayInfo.DisplayName,
                 *  AppUserModelId = info.AppInfo.AppUserModelId,
                 * });*/
            }

            foreach (var info in ProcessDiagnosticInfo.GetForProcesses())
            {
                /*ProcessItems.Add(new ProcessInfo
                 * {
                 *  ExecutableFileName = info.ExecutableFileName,
                 *  ProcessId = $"{info.ProcessId}",
                 *  CpuUsage = $"{info.CpuUsage.GetReport().UserTime:hh':'mm':'ss}",
                 *  MemoryUsage = $"{info.MemoryUsage.GetReport().WorkingSetSizeInBytes:N0}",
                 * });*/
            }
        }
        /// <summary>
        /// request for app diagnostic info
        /// </summary>
        private async void LoadAppInfo()
        {
            mainViewModel.AppInfoList.Clear();
            IList <AppDiagnosticInfo> list = await AppDiagnosticInfo.RequestInfoAsync();

            list.ToList().ForEach(o => mainViewModel.AppInfoList.Add(new AppInfoModel(o.AppInfo)));
        }
        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();
            }
        }
Ejemplo n.º 4
0
        public bool Update()
        {
            bool result = false;

            // If we don't lock the collection, there's a race where databinding could end up with
            // additional spurious instances, especially if the polling interval is only 1-2 sec.
            lock (lockObject)
            {
                // Get a list of running apps (filtered by permission level).
                IAsyncOperation <IList <AppDiagnosticInfo> > infoOperation = AppDiagnosticInfo.RequestInfoAsync();
                Task <IList <AppDiagnosticInfo> >            infoTask      = infoOperation.AsTask();
                infoTask.Wait();
                IList <AppDiagnosticInfo> runningApps = infoTask.Result;

                // We don't recreate the list each time: instead, we remove dead rows and update existing ones,
                // only adding new rows for new apps that appear. This is slow, but typically we'd only have a small
                // number of UWP apps running (say 5-20), so it's not noticeable.
                if (runningApps != null)
                {
                    // First, remove from the list any apps that are no longer in memory.
                    RemoveDeadRows(runningApps);

                    // Find key data from the bottom-up (process and group level) for roll-up purposes.
                    foreach (AppDiagnosticInfo app in runningApps)
                    {
                        ulong              appCommitLimit    = 0;
                        ulong              appTotalCommit    = 0;
                        ulong              appPrivateCommit  = 0;
                        ExecutionStateEx   appExecutionState = ExecutionStateEx.Unknown;
                        EnergyQuotaStateEx appEnergyState    = EnergyQuotaStateEx.Unknown;
                        int appBgTaskCount = 0;

                        // Get the roll-up data from group and process level.
                        IList <AppResourceGroupInfo> groups = app.GetResourceGroups();
                        if (groups != null)
                        {
                            foreach (AppResourceGroupInfo group in groups)
                            {
                                Debug.WriteLine("app: {0}, group Id: {1}", app.AppInfo.DisplayInfo.DisplayName, group.InstanceId);

                                // Get the total private commit usage of all processes for this app.
                                ulong totalProcessPrivateCommit         = 0;
                                IList <ProcessDiagnosticInfo> processes = group.GetProcessDiagnosticInfos();
                                if (processes != null)
                                {
                                    foreach (ProcessDiagnosticInfo process in processes)
                                    {
                                        totalProcessPrivateCommit += GetProcessPrivateCommit(process);
                                    }
                                }

                                // Accumulate the aggregated totals for all resource groups for this app.
                                // We pass down the private commit for all processes for this group for comparison only.
                                RowAggregateData groupData = GetGroupAggregateData(
                                    app.AppInfo.DisplayInfo.DisplayName, group, totalProcessPrivateCommit);

                                appCommitLimit   += groupData.CommitLimit;
                                appTotalCommit   += groupData.TotalCommit;
                                appPrivateCommit += groupData.PrivateCommit;
                                appBgTaskCount   += groupData.BgTaskCount;

                                if (appExecutionState != groupData.ExecutionState)
                                {
                                    if (appExecutionState == ExecutionStateEx.Unknown)
                                    {
                                        appExecutionState = groupData.ExecutionState;
                                    }
                                    else
                                    {
                                        appExecutionState = ExecutionStateEx.Multiple;
                                    }
                                }
                                if (appEnergyState != groupData.EnergyState)
                                {
                                    if (appEnergyState == EnergyQuotaStateEx.Unknown)
                                    {
                                        appEnergyState = groupData.EnergyState;
                                    }
                                    else
                                    {
                                        appEnergyState = EnergyQuotaStateEx.Multiple;
                                    }
                                }
                            }
                        }

                        RowAggregateData appData = new RowAggregateData(
                            appCommitLimit, appTotalCommit, appPrivateCommit, appBgTaskCount, appExecutionState, appEnergyState);

                        // Now add or update the rows.
                        bool isAppAdded = AddOrUpdateApp(app, appData, appExecutionState);
                        if (isAppAdded)
                        {
                            // If any row update resulted in adding an app, we set the return value to true.
                            result = true;
                        }
                    }
                }
            }
            return(result);
        }