Beispiel #1
0
        private void AddProcess(ProcessData NewProcess)
        {
            //ListViewItem NewProcessItem = ProcessView.Items.Add(NewProcess.Name);

            //NewProcessItem.SubItems.Add(NewProcess.ID.ToString());
            //NewProcessItem.SubItems.Add("0%");

            //NewProcess.ProcessItem = NewProcessItem;

            //if (NewProcess.ID == 0)
            //    IdleProcessItem = NewProcessItem;
        }
Beispiel #2
0
        private void GetUsage()
        {
            ProcessEntry32 ProcessInfo = new ProcessEntry32();
            ProcessTimes   ProcessTimes = new ProcessTimes();
            IntPtr         ProcessList, ProcessHandle = ProcessCPU.PROCESS_HANDLE_ERROR;
            ProcessData    CurrentProcessData;
            int            Index;
            int            Total = 0;
            bool           NoError;

            // this creates a pointer to the current process list
            ProcessList = ProcessCPU.CreateToolhelp32Snapshot(ProcessCPU.TH32CS_SNAPPROCESS, 0);

            if (ProcessList == ProcessCPU.PROCESS_LIST_ERROR)
            {
                return;
            }

            // we usage Process32First, Process32Next to loop threw the processes
            ProcessInfo.Size = ProcessCPU.PROCESS_ENTRY_32_SIZE;
            NoError          = ProcessCPU.Process32First(ProcessList, ref ProcessInfo);
            IDList.Clear();

            //ProcessView.SuspendLayout();

            while (NoError)
            {
                try
                {
                    // we need a process handle to pass it to GetProcessTimes function
                    // the OpenProcess function will provide us the handle by the id
                    ProcessHandle = ProcessCPU.OpenProcess(ProcessCPU.PROCESS_ALL_ACCESS, false, ProcessInfo.ID);

                    // here's what we are looking for, this gets the kernel and user time
                    ProcessCPU.GetProcessTimes(
                        ProcessHandle,
                        out ProcessTimes.RawCreationTime,
                        out ProcessTimes.RawExitTime,
                        out ProcessTimes.RawKernelTime,
                        out ProcessTimes.RawUserTime);

                    // convert the values to DateTime values
                    ProcessTimes.ConvertTime();

                    //from here is just managing the gui for the process list
                    CurrentProcessData = ProcessExists(ProcessInfo.ID);
                    IDList.Add(ProcessInfo.ID);

                    if (CurrentProcessData == PROCESS_DATA_NOT_FOUND)
                    {
                        Index = ProcessDataList.Add(new ProcessData(
                                                        ProcessInfo.ID,
                                                        ProcessInfo.ExeFilename,
                                                        ProcessTimes.UserTime.Ticks,
                                                        ProcessTimes.KernelTime.Ticks));

                        AddProcess((ProcessData)ProcessDataList[Index]);
                    }
                    else
                    {
                        //TEST inline condition
                        Total += (CrcService.Properties.Settings.Default.CPUUseSwitch ? CurrentProcessData.UpdateCpuUsage(
                                      ProcessTimes.UserTime.Ticks,
                                      ProcessTimes.KernelTime.Ticks) : 0);
                    }
                }
                finally
                {
                    if (ProcessHandle != ProcessCPU.PROCESS_HANDLE_ERROR)
                    {
                        ProcessCPU.CloseHandle(ProcessHandle);
                    }

                    NoError = ProcessCPU.Process32Next(ProcessList, ref ProcessInfo);
                }
            }

            ProcessCPU.CloseHandle(ProcessList);

            Index = 0;

            DateTime dt = (System.DateTime)DateTime.Now;

            while (Index < ProcessDataList.Count)
            {
                ProcessData          TempProcess = (ProcessData)ProcessDataList[Index];
                dsProcess.ProcessRow row;
                row            = pdt.NewProcessRow();
                row.CompName   = compName;       //GetCompName();
                row.CompUser   = compUser;       //GetCompUser();
                row.IP         = localIPAddress; //GetIPAddress(); // GetLocalIpAddress();
                row.ProcesName = TempProcess.Name;
                row.CPUUse     = TempProcess.CpuUsage;
                row.ProcTime   = dt;
                row.ProcID     = (Int32)TempProcess.ID;

                string   sProzori = String.Empty;
                IntPtr[] prozori  = GetProcessWindows((int)TempProcess.ID);
                Int32[]  lprozori = new Int32[prozori.Length];
                for (int j = 0; j < prozori.Length; j++)
                {
                    sProzori   += "@" + prozori[j].ToInt32().ToString() + "@";
                    lprozori[j] = prozori[j].ToInt32();
                }

                Int32[] commonElements        = comm.iHwnd.Intersect(lprozori).ToArray();
                Int32   commonElementsCounter = comm.iHwnd.Count() - comm.iHwnd.Except(lprozori).Count();
                if (commonElements.Length > 0)
                {
                    row.counter    = (short)commonElementsCounter;
                    row.currWindID = commonElements[0].ToString();
                }

                row.hWindID = sProzori;

                if (row.counter > 0)
                {
                    pdt.AddProcessRow(row);
                    pdt.AcceptChanges();
                    LogTo("ADD  " + row.ProcID + "  " + row.ProcTime, true);
                }
                else
                {
                    //pdt.RemoveProcessRow(row);
                    row.Delete();
                    row = null;
                    pdt.RejectChanges();
                }


                if (IDList.Contains(TempProcess.ID))
                {
                    Index++;
                }
                else
                {
                    //ProcessView.Items.Remove(TempProcess.ProcessItem);
                    ProcessDataList.RemoveAt(Index);
                }
            }

            //IdleProcessItem.SubItems[2].Text = (100 - Total) + "%";
            //ProcessView.ResumeLayout();

            comm.ResetCounter();

            SaveXml();

            ShowCountData();
        }