C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




How to retrieve the running system processes?


Posted Date: 18 Jun 2007    Resource Type: Code Snippets    Category: Process

Posted By: Abhishek Arya       Member Level: Diamond
Rating:     Points: 10



This methos provide the same information for the system processes as the well known Windows Task Manager.


public DataSet getProcesses()
{
/*
Feel free to use and redistribute this method
in condition that you keep this message intact.
*/

Process[] procs;
TimeSpan cputime;

procs = Process.GetProcesses();

DataSet myDataSet = new DataSet("myDataSet");

DataTable tProc = new DataTable("nc");

// name, pid, time, mem, peakmem, handles, threads;

DataColumn pName = new DataColumn("name", typeof(string));
DataColumn pPid = new DataColumn("pid", typeof(string));
DataColumn pTime = new DataColumn("time", typeof(string));
DataColumn pMem = new DataColumn("mem", typeof(string));
DataColumn pPeakmem = new DataColumn("peakmem", typeof(string));
DataColumn pHandles = new DataColumn("handles", typeof(string));
DataColumn pThreads = new DataColumn("threads", typeof(string));



tProc.Columns.Add(pName);
tProc.Columns.Add(pPid);
tProc.Columns.Add(pTime);
tProc.Columns.Add(pMem);
tProc.Columns.Add(pPeakmem);
tProc.Columns.Add(pHandles);
tProc.Columns.Add(pThreads);


myDataSet.Tables.Add(tProc);


string name, pid, time, mem, peakmem, handles, threads;

DataRow newRow2;

foreach(Process proc in procs)
{


proc.Refresh();

cputime = proc.TotalProcessorTime;

name = proc.ProcessName;

pid = proc.Id.ToString();

time = String.Format(
"{0}:{1}:{2}",
((cputime.TotalHours-1<0?0:cputime.TotalHours-1)).ToString("##0"),
cputime.Minutes.ToString("00"),
cputime.Seconds.ToString("00")
);


mem = (proc.WorkingSet/1024).ToString()+"k";

peakmem = (proc.PeakWorkingSet/1024).ToString()+"k";

handles = proc.HandleCount.ToString();

threads = proc.Threads.Count.ToString();

newRow2 = tProc.NewRow();

newRow2["name"]= name;
newRow2["pid"]= pid;
newRow2["time"]= time;
newRow2["mem"]= mem;
newRow2["peakmem"]= peakmem;
newRow2["handles"]= handles;
newRow2["threads"]= threads;

tProc.Rows.Add(newRow2);
proc.Close();
}

procs = null;

return myDataSet;
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Find all running applications and processes in the system
Previous Resource: Finding System Idle Time and ur Customized form as display Screen saver
Return to Discussion Resource Index
Post New Resource
Category: Process


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

efax

Contact Us    Privacy Policy    Terms Of Use