コード例 #1
0
        private void refresh()
        {
            ProcessEntry32 processInfo = new ProcessEntry32();
            ProcessTimes   processTimes = new ProcessTimes();
            IntPtr         processList, processHandle = ProcessCPU.PROCESS_HANDLE_ERROR;
            ProcessData    currentProcessData;
            int            index, total = 0;
            bool           noError;

            processList = ProcessCPU.CreateToolhelp32Snapshot(ProcessCPU.TH32CS_SNAPPROCESS, 0);
            if (processList == ProcessCPU.PROCESS_LIST_ERROR)
            {
                return;
            }
            processInfo.Size = ProcessCPU.PROCESS_ENTRY_32_SIZE;
            noError          = ProcessCPU.Process32First(processList, ref processInfo);
            IdList.Clear();
            while (noError)
            {
                try
                {
                    processHandle = ProcessCPU.OpenProcess(ProcessCPU.PROCESS_ALL_ACCESS, false, processInfo.ID);
                    ProcessCPU.GetProcessTimes(
                        processHandle,
                        out processTimes.RawCreationTime,
                        out processTimes.RawExitTime,
                        out processTimes.RawKernelTime,
                        out processTimes.RawUserTime);

                    processTimes.ConvertTime();
                    currentProcessData = processExists(processInfo.ID);
                    IdList.Add(processInfo.ID);
                    if (currentProcessData == null)
                    {
                        index = ProcessDataList.Add(new ProcessData(
                                                        processInfo.ID,
                                                        processInfo.ExeFilename,
                                                        processTimes.UserTime.Ticks,
                                                        processTimes.KernelTime.Ticks));
                        if (processInfo.ID != 0)
                        {
                            AddProcess((ProcessData)ProcessDataList[index]);
                        }
                    }
                    else
                    {
                        total += currentProcessData.UpdateCpuUsage(
                            processTimes.UserTime.Ticks,
                            processTimes.KernelTime.Ticks);
                    }
                }
                finally
                {
                    if (processHandle != ProcessCPU.PROCESS_HANDLE_ERROR)
                    {
                        ProcessCPU.CloseHandle(processHandle);
                    }
                    noError = ProcessCPU.Process32Next(processList, ref processInfo);
                }
            }

            ProcessCPU.CloseHandle(processList);
            index = 0;
            while (index < ProcessDataList.Count)
            {
                ProcessData TempProcess = (ProcessData)ProcessDataList[index];
                if (IdList.Contains(TempProcess.Id))
                {
                    index++;
                }
                else
                {
                    Processes.Remove(TempProcess);
                    ProcessDataList.RemoveAt(index);
                }
            }
            NotifyPropertyChanged("Processes");
        }
コード例 #2
0
 public static extern bool Process32Next(IntPtr Handle, ref ProcessEntry32 ProcessInfo);