Click here to Skip to main content
15,896,544 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 92.1K   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;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    [RunInstaller(true)]
    public class CPUMonitorInstaller : System.Configuration.Install.Installer
    {
        #region Constructors

        public CPUMonitorInstaller()
            : base()
        {
            this.Committed += new InstallEventHandler(MyInstaller_Committed);
            this.Committing += new InstallEventHandler(MyInstaller_Committing);
        }

        #endregion Constructors

        #region Methods

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
        }

        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            foreach (Process process in Process.GetProcessesByName("CPUMonitor"))
            {
                process.CloseMainWindow();
                Thread.Sleep(10000);
                try
                {
                    if (!process.HasExited)
                        process.Kill();

                    process.Close();
                }
                catch
                {
                }
            }
        }

        private void MyInstaller_Committed(object sender, InstallEventArgs e)
        {
            try
            {
                Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                Process.Start(Path.GetDirectoryName(
                    Assembly.GetExecutingAssembly().Location) + "\\CPUMonitor.exe");
            }
            catch
            {
                MessageBox.Show("There was a problem launching the application. Please restart your computer to run CPU Monitor",
                    "Problem launching CPU Monitor", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void MyInstaller_Committing(object sender, InstallEventArgs e)
        {
        }

        #endregion Methods
    }
}

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