Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: cpu Task
I want to store the CPU and Memory Utilization of all the individual tasks after every 5 mins. from the task manager into a DB, say SQL Database. Is it possible to write a console application in C# which can accomplish this task and store the utilization in SQL Table.
Posted 22-Apr-13 5:14am
Abhik03368
Comments
Mike Meinz at 22-Apr-13 10:20am
   
You could try using the WMI Code Creator to create some code to get the values you need via WMI and then integrate that code into a program that puts the data retrieved into the database.
Sergey Alexandrovich Kryukov at 22-Apr-13 12:10pm
   
It would make a fair answer. Will you post it?
I just answered on another way, using performance counters...
—SA
Sergey Alexandrovich Kryukov at 22-Apr-13 12:09pm
   
Why? You should understand that the accuracy of Task Manager data is very low. And of course, you cannot get this data from Task Manager, but you can request if from OS.
—SA
Abhik03 at 23-Apr-13 1:20am
   
Thanks for you suggetsion Sergey and Mike, I'll try them and will get back to you in case of any doubts. :)
Sergey Alexandrovich Kryukov at 23-Apr-13 9:47am
   
I suggest you can accept the answers formally (green button) — thanks.
—SA
Abhik03 at 23-Apr-13 6:10am
   
Hi Sergey, I have tried the below piece of code, but it shows some different value with the task manager, pls help:
 
static void Main(string[] args)
{
int i = 0;
try
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
 
string[] instanceNames = cat.GetInstanceNames();


foreach (string name in instanceNames)
{

try
{

PerformanceCounter counter = new PerformanceCounter("Process", "Private Bytes", name, true);
PerformanceCounter setCounter = new PerformanceCounter("Process", "Working Set", name, true);
PerformanceCounter poolNPCounter = new PerformanceCounter("Process", "Pool Nonpaged Bytes", name, true);
 
Console.WriteLine("Memory usage for process [{0}]", name);
Console.WriteLine("\tMem Usage: {0} KB", (setCounter.NextValue() / 1024f));
Console.WriteLine("\tVM Size: {0} KB", (counter.NextValue() / 1024f));
Console.WriteLine("\tNon-Paged Pool: {0} KB", (poolNPCounter.NextValue() / 1024f));
i++;
}
catch (Exception ex)
{
Console.WriteLine("Could not read memory stats for process {0}", name);
}
}
}
catch (Exception ex2)
{
Console.WriteLine("Cannot retrieve memory performance counter statistics");
}
Console.WriteLine("Total processes: " + i);
Console.ReadLine();
}
Sergey Alexandrovich Kryukov at 23-Apr-13 9:43am
   
I would say, you can ignore Task Manager results...
—SA
Abhik03 at 24-Apr-13 2:22am
   
Ok sure, Thanks Sergey :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can get this information is you use System.Diagnostics.PerformanceCounter:
http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx[^].
 
You can use this introductory CodeProject article to see how to do things: An Introduction To Performance Counters[^].
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Actually all the information you got so far is correct:
- Task manager is just a tool to display data. It is not collecting anything
- In the background of task manager (and others like this) are system level tools, like WMI, as Mike Meinz highlighted.
- And you can access them easily also with performance counter tools provided by the .net framework as Sergey suggested.
 
Still, I would like to draw your attention towards built-in features you can use to achieve much the same result with no code at all from your side. The well-known PerfMon tool has features you can use to store your results into a database. Please see following article: http://www.sepago.de/e/nicholas/2009/11/02/performance-monitoring-part-7-using-performance-monitor-with-a-database[^].
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 22-Apr-13 15:35pm
   
Agree, a 5.
—SA
Zoltán Zörgő at 22-Apr-13 17:03pm
   
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100