2012-04-16 109 views
4

我正在運行一個測試,啓動兩個進程,使用C#。我需要獲得我的進程使用的最高內存和CPU。請有人可以給我一個關於如何使用託管代碼做的指導? (我也使用mono在linux上運行它)。如何配置內存和CPU使用率從C#

架構如下:

方法test.exe啓動兩個過程:A.exeB.exe。我需要衡量流程A和B meausre最大內存和CPU,從test.exe

是否有可能呢?在此先感謝

+0

頂部CPU?你的意思是百分比嗎?運行時,它是其核心的100%。當它被阻止時,它是0%。進程的CPU使用率百分比是這兩者的運行平均值。那真的是你想知道的嗎? – 2012-04-16 16:52:13

回答

7

您可以使用System.Diagnostics.Process類來啓動該過程。然後,您可以檢查UserProcessorTime,TotalProcessorTime,PeakWorkingSet64和其他屬性以檢查處理器使用情況和內存使用情況。檢查這個MSDN Article for System.Diagnostics.Process

+0

這不會告訴你處理器的使用情況,只有CPU時間。 – Joey 2012-04-16 11:24:53

2

嘗試使用這種Get CPU Info

PerformanceCounter cpuCounter; 
PerformanceCounter ramCounter; 

cpuCounter = new PerformanceCounter(); 

cpuCounter.CategoryName = "Processor"; 
cpuCounter.CounterName = "% Processor Time"; 
cpuCounter.InstanceName = "_Total"; 

ramCounter = new PerformanceCounter("Memory", "Available MBytes"); 


public string getCurrentCpuUsage(){ 
     return cpuCounter.NextValue()+"%"; 
} 

public string getAvailableRAM(){ 
     return ramCounter.NextValue()+"MB"; 
} 

編輯: 所以,你可以檢查的區別你開始你的過程,前後。

0

或者你可以嘗試使用這種

private ulong GetCPU(ThreadEntry[] thList) 
{ 
    ulong result = 0; 
    for (int i = 0; i < thList.Length; i++) 
    { 
    ulong NewThC = 0; 
    ulong NewThE = 0; 
    ulong NewThK = 0; 
    ulong NewThU = 0; 
    if(GetThreadTimes(thList[i].ThreadID, ref NewThC, ref NewThE, ref NewThK, ref NewThU)) 
     result = result + NewThK + NewThU; 
    int error = Marshal.GetLastWin32Error(); 
//often ERROR == 6 or ERROR == 18 
    } 
    return result; 
} 

//GET ALL PROCESS CPU USAGE 
private void timer1_Tick(object sender, EventArgs e) 
{ 
//reset results 
    this.textBox1.Text = string.Empty; 
//turn of timer 
    this.Enabled = false; 
    uint perm = GetCurrentPermissions(); 


    //SET PERMISION SO I CAN RECEIVE INFO ABOUT THREADS 
    SetProcPermissions(0xFFFFFFFF); 


//get all running process (OPENNETCF)  
    List<ProcessEntry> processList = new List<ProcessEntry>(ProcessEntry.GetProcesses()); 

//OLD Variables stored in list 
    if (OldResList == null || (OldResList != null && OldResList.Length != processList.Count)) 
    OldResList = new ulong[processList.Count]; 

//SORT by ID only for testing  
    processList.Sort(CompareProcessEntry); 
//GET ALL CPU USAGE  
    for(int i=0;i<processList.Count;i++) 
    { 
    //new value 
    ulong newRes = GetCPU(processList[i].GetThreads()); 
    //result 
    ulong result = (newRes - OldResList[i]); 
    //valid result 
    result = result/(ulong)this.timer1.Interval; 
    //set result to the thexbox 
    this.textBox1.Text += processList[i].ExeFile + " " + result.ToString() + " ;"; 
    //change old to new 
    OldResList[i] = newRes; 
    } 
    //sleep 
    Thread.Sleep(1000); 
    SetProcPermissions(0xFFFFFFFF); 
    //start again 

    timer1.Enabled = true; 
    } 
    public static int CompareProcessEntry(ProcessEntry p1, ProcessEntry p2) 
    { 
    return p1.ProcessID.CompareTo(p2.ProcessID); 
    } 

    [DllImport("coredll")] 
    private static extern bool GetThreadTimes(uint p, ref ulong NewThC, ref ulong NewThE, ref ulong NewThK, ref ulong NewThU);