Ejemplo n.º 1
0
 public static extern bool Process32Next(IntPtr Handle, ref ProcessEntry32 ProcessInfo);
Ejemplo n.º 2
0
        private void GetUsage()
        {
            ProcessEntry32 ProcessInfo = new ProcessEntry32();
            ProcessTimes   ProcessTimes = new ProcessTimes();
            IntPtr         ProcessList, ProcessHandle = ProcessCPU.PROCESS_HANDLE_ERROR;
            ProcessData    CurrentProcessData;
            int            Index = 0;
            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 += (CrcMrc.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;
                row.Title      = GetTitleWindow((Int32)TempProcess.ID); //TEST TEST TEST

                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;

                /* counter prozora za gledanje */
                Int32[] commonElementsView        = comm.iHwndView.Intersect(lprozori).ToArray();
                Int32   commonElementsCounterView = comm.iHwndView.Count() - comm.iHwndView.Except(lprozori).Count();
                //if (commonElementsView.Length > 0 && commonElements.Length == 0)
                if (commonElementsView.Length > 0)
                {
                    row.counter    = (short)commonElementsCounterView;
                    row.currWindID = commonElementsView[0].ToString();
                    row.ViewOnly   = true;
                }


                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();
        }