Exemplo n.º 1
0
        private static async Task <ProcessWrapper> BuildNew(Process p)
        {
            ProcessWrapper res = new ProcessWrapper();

            res.Pid      = p.Id;
            res.Name     = p.ProcessName;
            res.IsActive = p.Responding;
            res.UserName = GetUserName(res.Pid);
            string fullPath = p.MainModule.FileName;

            res.FilePath = fullPath.Substring(0, fullPath.LastIndexOf('\\'));
            res.FileName = fullPath.Substring(fullPath.LastIndexOf('\\') + 1);
            PerformanceCounter cpuPc = new PerformanceCounter("Process", "% Processor Time", res.Name, true);

            cpuPc.NextValue();
            lock (Lock)
            {
                ProcessCache.Add(res.Pid,
                                 new ProcessData(res.Name, res.IsActive, res.UserName, res.FilePath, res.FileName, cpuPc));
            }
            await Task.Delay(1000);

            return(res);
        }
Exemplo n.º 2
0
        internal static async Task <ProcessWrapper> ConstructProcessWrapper(Process p)
        {
            try
            {
                ProcessWrapper instance = ProcessCache.ContainsKey(p.Id)
                    ? UseCache(p.Id)
                    : await Task.Run(() => BuildNew(p));

                instance.SetMutableData(p);
                return(instance);
            }
            catch (Win32Exception)
            {
                return(new ProcessWrapper {
                    Pid = p.Id, Name = null
                });
            }
            catch (Exception)
            {
                return(new ProcessWrapper {
                    Pid = -1, Name = null
                });
            }
        }