J'ai besoin d'appeler précis d'utilisation de l'UC d'un seul processus

Le Truc est que j'ai aussi besoin d'être en mesure de le faire sur multi core machines. Ma formation en C# est un peu cassé. J'ai réussi le code suivant. Quelqu'un peut-il m'aider? Iv essayé d'utiliser le "_Total" drapeau et j'ai essayé de modifier quelques autres extraits de code qui ressemblaient ils ont essayé de détecter la quantité de noyaux. On m'a dit cependant, ils ne comprennent pas l'HT et pris en charge uniquement physique pas processeurs logiques. J'essayais de lui faire faire les deux. Apparemment, leur est une façon de faire manuellement ce à l'aide de

    ("Process", "% Processor Time", "1" process.ProcessName))
    ("Process", "% Processor Time", "2" process.ProcessName))
    ("Process", "% Processor Time", "3" process.ProcessName))

etc. Mais j'ai trouvé le matériel qui ne fonctionne pas si les carottes ne pas exister. J'espérais que je pourrais venir à travers quelque chose de plus souple. Iv travaillé sur ce projet pendant des jours, des heures et des heures à la fois et je vais tirer mes cheveux.

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.IO;
namespace Program_CPU_Monitor
{
class Program
{
static void Main(string[] args)
{
StreamWriter log;
log = File.AppendText("c:\\CPUMON.txt");
log.WriteLine("");
log.WriteLine("**Started logging Program CPU Monitor (2.6.0.63)**");
log.Close();
Console.Title = "Program CPU Monitor 2.6.0.63";
Console.WriteLine("Monitoring Program CPU & Memory usage...(1-min intervals)");
Console.WriteLine("Monitoring will start when Program is detected as running.");
Console.WriteLine("Please type in program name without the '.EXE', For example 'TESV' or 'calc'.");
Console.WriteLine("The program name is case sensative. Without the proper case it will not work.");
Console.WriteLine("This program will leave a log of the display called 'CPUMON.txt' on drive C:/.");
Console.WriteLine("Please type program name...");
Console.WriteLine(""); 
string procName = Console.ReadLine();
while (true)
{
Process[] runningNow = Process.GetProcesses();
foreach (Process process in runningNow)
{
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
pcProcess.NextValue();
Thread.Sleep(60000);
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
OurStream.WriteLine("Process: '{0}' CPU Usage: {1}%", process.ProcessName, pcProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
OurStream.WriteLine("Process: '{0}' RAM Free: {1}MB", process.ProcessName, memProcess.NextValue());
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.WriteLine(string.Format("Recorded: '{0}' at {1}", procName, DateTime.Now.ToString()));
OurStream.Close();
}
}
}
}
}
}
}

EDIT:: j'ai fait les modifications suivantes au code pour résoudre mes problèmes par des conseils et de l'administration fiddeling autour de.

    foreach (Process process in runningNow)
{
using (PerformanceCounter cpuUsage = new PerformanceCounter("Process", "% Processor Time", "_Total"))
using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
using (PerformanceCounter memProcess = new PerformanceCounter("Memory", "Available MBytes"))
{
if (process.ProcessName == procName)
{
StreamWriter OurStream;
OurStream = File.AppendText("c:\\CPUMON.txt");
Console.WriteLine("");
OurStream.WriteLine("");
//Prime the Performance Counters
pcProcess.NextValue();
cpuUsage.NextValue();
Thread.Sleep(100);
isprimed = true;
double cpuUse = Math.Round(pcProcess.NextValue() / cpuUsage.NextValue() * 100, 2);
//Check for Not-A-Number (Division by Zero)
if (Double.IsNaN(cpuUse))
cpuUse = 0;
//Get CPU Usage
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
OurStream.WriteLine("Process: `{0}' CPU Usage: {1}%", process.ProcessName, Convert.ToInt32(cpuUse));
//Get Process Memory Usage
Console.ForegroundColor = ConsoleColor.Green;
double memUseage = process.PrivateMemorySize64 / 1048576;
Console.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
OurStream.WriteLine("Process: `{0}' Memory Usage: {1}MB", process.ProcessName, memUseage);
//Get Total RAM free
Console.ForegroundColor = ConsoleColor.Cyan;
float mem = memProcess.NextValue();
Console.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
OurStream.WriteLine("During: `{0}' RAM Free: {1}MB", process.ProcessName, mem);
//Record and close stream
Console.ForegroundColor = ConsoleColor.Yellow;
System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("Recorded: {0}", newDate);
OurStream.WriteLine("Recorded: {0}", newDate);
OurStream.Close();
Thread.Sleep(59900);

OriginalL'auteur Solaris17 | 2011-12-11