Windows Process Monitoring Tool (1)--windows process acquisition

Source: Internet
Author: User
Tags cpu usage
DescriptionTo monitor the utilization of resources (memory, CPU usage) under Windows, use C # to make a small tool here. For example: Under Window, to do some performance testing, you need to monitor the process memory and CPU usage, although there are many methods of monitoring, generally can only see the process to occupy resources trend, unable to obtain accurate data for comparison. For example, when monitoring a process, let the tool record the process memory and CPU usage per second, and then output the data at the end of the monitoring. Here, homemade. The tool is divided into three steps: Windows process acquisition, CPU usage algorithms, and monitoring data output.
Get Process
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using System.Diagnostics;
Using System.IO;

Using System.Threading;
         namespace Processmonitor {public partial class Form1:form {/** * initialization window.

            * * Public Form1 () {InitializeComponent ();
            Sets the three column headings in the ListView.
            LISTVIEW1.COLUMNS.ADD ("ProcessID", 80);
            LISTVIEW1.COLUMNS.ADD ("ProcessName", 100);

            LISTVIEW1.COLUMNS.ADD ("StartTime", 170);
            Displays the lines of the matrix.

            Listview1.gridlines = true;
            Displays the details of the attempt.

            Listview1.view = View.Details;
            The row you are trying to select.

            Listview1.fullrowselect = true;
            Gets the current all processes.

            Process[] processes = process.getprocesses (); foreach (process p in processes) {//Get three needs display for each processTo the properties in the window.
                String ProcessID = P.id.tostring ();
                string processname = P.processname;

                string starttime = "";
                If some process start time cannot be obtained, it is set to its exception information.
                try {starttime = GetStartTime (p.starttime); The catch (Exception ex) {starttime = ex.
                message;
                //Add three attributes to the display list.
                ListViewItem item = new ListViewItem (new string[] {ProcessID, processname,starttime});
            LISTVIEW1.ITEMS.ADD (item);
         }/** * Converts the calendar time to a string-type time for easy display.
            * * Private string GetStartTime (DateTime datetime) {string starttime = null;
            String year = DateTime.Year.ToString ();
            String month = DateTime.Month.ToString ();

            String day = DateTime.Day.ToString (); String hour = DateTime.Hour.ToString ();
            String minute = DateTime.Minute.ToString ();

            string second = DateTime.Second.ToString ();

            StartTime = year + "-" + month + "-" + Day + "" + Hour + ":" + Minute + ":" + second;
        return starttime;
         /** the Update button in the * window to refresh the process displayed in the window.
            * * private void Button1_Click (object sender, EventArgs e) {//refresh, clear all content.

            ListView1.Items.Clear ();
            Gets the current all processes.
            Process[] processes = process.getprocesses ();
                foreach (process p in processes) {//Get three properties of each process that need to be displayed to the window.
                String ProcessID = P.id.tostring ();

                string processname = P.processname;
                If some process start time cannot be obtained, it is set to its exception information.
                string starttime = null;
                try {starttime = GetStartTime (p.starttime); catch (exception ex) {starttime = ex.
                message;
                //Add three attributes to the display list.
                ListViewItem item = new ListViewItem (new string[] {ProcessID, processname, starttime});
            LISTVIEW1.ITEMS.ADD (item); }

        }

    }
}
window Effects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.