.NET to obtain the current program where the computer's CPU and memory usage

275 people read comment(0) Collection Report

System using;
System.Diagnostics using;
System.Text using;
System.Runtime.InteropServices using;

ConsoleApplication1 namespace

Program class
Static PerformanceCounter CPU public;
Static ComputerInfo CIF //public;
Void Main static (args string[])
CPU = PerformanceCounter new ("Processor", "% Time Processor", "_Total");
/ / CIF = new (ComputerInfo);
MemInfo = MEMORY_INFO new ();
While (true)
GlobalMemoryStatus (MemInfo ref);
Console.WriteLine (MemInfo.dwMemoryLoad.ToString) + "% of memory is being used");
Percentage var = cpu.NextValue ();
Console.WriteLine (percentage + "% CPU is using \n");
System.Threading.Thread.Sleep (2000);

[DllImport ("Kernel32")
Static extern void GetSystemDirectory (SysDir StringBuilder, count int), public ();
[DllImport ("Kernel32")
Static extern void GetSystemInfo public (CPU_INFO cpuinfo ref);
[DllImport ("Kernel32")
Static extern void GlobalMemoryStatus public (MEMORY_INFO meminfo ref);
[DllImport ("Kernel32")
Static extern void GetSystemTime public (SYSTEMTIME_INFO stinfo ref);
The definition of CPU / / information structure
[StructLayout (LayoutKind.Sequential)]
Struct CPU_INFO public
Uint dwOemId public;
Uint dwPageSize public;
Uint lpMinimumApplicationAddress public;
Uint lpMaximumApplicationAddress public;
Uint dwActiveProcessorMask public;
Uint dwNumberOfProcessors public;
Uint dwProcessorType public;
Uint dwAllocationGranularity public;
Uint dwProcessorLevel public;
Uint dwProcessorRevision public;
/ / information structure definition memory
[StructLayout (LayoutKind.Sequential)]
Struct MEMORY_INFO public
Uint dwLength public;
Uint dwMemoryLoad public;
Uint dwTotalPhys public;
Uint dwAvailPhys public;
Uint dwTotalPageFile public;
Uint dwAvailPageFile public;
Uint dwTotalVirtual public;
Uint dwAvailVirtual public;
/ / information system time structure definition
[StructLayout (LayoutKind.Sequential)]
USHORT wYear public;
USHORT wMonth public;
USHORT wDayOfWeek public;
USHORT wDay public;
USHORT wHour public;
USHORT wMinute public;
USHORT wSecond public;
USHORT wMilliseconds public;

You can cycle to judge the CPU and memory usage, to do the corresponding functions, such as Thunderbolt free download is and the same is computer CPU and memory are idle time, some work to do lest people in the use of run at the same time cause the computer card.

step on
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit390746 times
    • Integral:Six thousand three hundred and four
    • Grade
    • Rank:2029th name
    • Original236
    • Reproduced:18
    • Translation:11
    • Comments:121
    Site search
    Latest comments