daily pastebin goal
6%
SHARE
TWEET

Untitled

a guest Jan 17th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Thread checkProcess;
  2. List<PerformanceCounter> pcs;
  3.    
  4. private void btnMonitor_Click(object sender, EventArgs e)
  5. {
  6.     List<Process> proc = new List<Process>();
  7.     foreach (Process p in Process.GetProcesses())
  8.     {
  9.         if (p.ProcessName.IndexOf(txtProcessName.Text) > -1)
  10.         {
  11.             proc.Add(p);
  12.         }
  13.     }
  14.     pcs = new List<PerformanceCounter>();
  15.     int k = 0;
  16.     foreach (Process p in proc)
  17.     {
  18.         pcs.Add(new PerformanceCounter("Process", "% Processor Time", k > 0 ?
  19.             string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName, true));
  20.         lstbxPC.Items.Add(k > 0 ?
  21.             string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName);
  22.         k++;
  23.     }
  24.     checkProcess = new Thread(CheckProcess);
  25.     checkProcess.Start();
  26. }
  27.    
  28. private void CheckProcess()
  29. {
  30.     int temp = 0;
  31.     while (true)
  32.     {
  33.         temp = 0;
  34.         foreach (PerformanceCounter pc in pcs)
  35.         {
  36.             if (temp < pc.NextValue())
  37.             {
  38.                 temp = (int)pc.NextValue();
  39.             }
  40.  
  41.         }
  42.         // Set the label in a ThreadSafe way. This does work
  43.         SetControlPropertyThreadSafe(lblCPU, "Text", Convert.ToString(temp) + "%");
  44.         Thread.Sleep(1000);
  45.     }
  46. }
  47.    
  48. private void btnMonitor_Click(object sender, EventArgs e)
  49.     {
  50.         var proc = Process.GetProcesses().Where(p => p.ProcessName.IndexOf(this.txtProcessName.Text) > -1);
  51.         var k = 1;
  52.         var appendSuffix = proc.Count() > 1;
  53.  
  54.         this.pcs = new List<PerformanceCounter>();
  55.         foreach (var p in proc)
  56.         {
  57.             this.pcs.Add(new PerformanceCounter(
  58.                 "Process",
  59.                 "% Processor Time",
  60.                 appendSuffix ? string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName,
  61.                 true));
  62.             this.lstbxPC.Items.Add(appendSuffix ? string.Format("{0}#{1}", p.ProcessName, k) : p.ProcessName);
  63.             k++;
  64.         }
  65.  
  66.         this.checkProcess = new Thread(this.CheckProcess);
  67.         this.checkProcess.Start();
  68.     }
  69.  
  70.     private void CheckProcess()
  71.     {
  72.         while (true)
  73.         {
  74.             var temp = 0;
  75.  
  76.             foreach (var pc in this.pcs)
  77.             {
  78.                 var nextValue = pc.NextValue();
  79.  
  80.                 if (temp < nextValue)
  81.                 {
  82.                     temp = (int)nextValue;
  83.                 }
  84.             }
  85.  
  86.             // Set the label in a ThreadSafe way. This does work.
  87.             SetControlPropertyThreadSafe(lblCPU, "Text", Convert.ToString(temp) + "%");
  88.             Thread.Sleep(1000);
  89.         }
  90.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top