Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Windows Forms

CPUAlert: Save your CPU from Burning Hot and Battery Running Out Quickly

Rate me:
Please Sign up or sign in to vote.
4.74/5 (20 votes)
11 Jun 2011CPOL5 min read 89.9K   1.6K   69  
CPUAlert monitors CPU and Memory consumption of processes and alerts you when they are taking too much consistently and gives you an option to recycle or terminate
namespace CPUMonitor
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    using CPUMonitor.Properties;

    public partial class KillProcessForm : Form
    {
        #region Fields

        private ProcessInfo _Process;

        #endregion Fields

        #region Constructors

        public KillProcessForm()
        {
            InitializeComponent();
        }

        #endregion Constructors

        #region Events

        public event Action<ProcessInfo> Ignore;

        public event Action<ProcessInfo> Postpone;

        public event Action<ProcessInfo> Restart;

        public event Action<ProcessInfo> Terminate;

        #endregion Events

        #region Methods

        public void Show(ProcessInfo process, string whatsHigh)
        {
            this._Process = process;

            this.Text = "Warning! Process taking high " + whatsHigh;
            this.TitleLabel.Text = "The following process is taking high " + whatsHigh + " and should be closed:";
            this.ProcessNameLabel.Text = process.Title;
            this.PostponeButton.Text = string.Format("Postpone {0} mins", Settings.Default.Default_Postpone_Time.TotalMinutes);
            this.CPULabel.Text = process.CpuUsage + "%";
            this.MemoryLabel.Text = (process.WorkingSet / 1048576) + " MB";
            try
            {
                Process realProcess = Process.GetProcessById(process.Id);
                this.ProcessNameLabel.Text = realProcess.MainWindowTitle;

            }
            catch
            {
            }

            this.Show();
            this.Refresh();
            this.TopMost = true;

            this.DummyTextBox.Focus();
        }

        private void IgnoreButton_Click(object sender, EventArgs e)
        {
            Ignore(this._Process);
        }

        private void KillProcessForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                Ignore(this._Process);
            }
        }

        private void PostponeButton_Click(object sender, EventArgs e)
        {
            Postpone(this._Process);
        }

        private void RestartButton_Click(object sender, EventArgs e)
        {
            Restart(this._Process);
        }

        private void TerminateButton_Click(object sender, EventArgs e)
        {
            Terminate(this._Process);
        }

        #endregion Methods

        private void KillProcessForm_Shown(object sender, EventArgs e)
        {
            this.DummyTextBox.Focus();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions