Return to Snippet

Revision: 36135
at November 19, 2010 05:04 by cabrel


Updated Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
 
namespace CpuWhiner
{
    class Program
    {
        //
        // To enable this to be 'user friendly' at start-up. We need to
        // suppress the console window.
        //
        // The following two methods will help us achieve this
        // FindWindow MSDN
        // http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
        //
        // ShowWindow MSDN
        // http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx
        //
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName,
                                                string lpWindowName);
 
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
        static void Main(string[] args)
        {
            // Give our window a unique title
            Console.Title = "CpuWhinerConsoleWindow";
 
            // Create our pointer to our console window
            // and if the window was found, hide it
            IntPtr hWnd = FindWindow(null, Console.Title);
            if (hWnd != IntPtr.Zero)
            {
                // Hide the console window
                ShowWindow(hWnd, 0);
            }
 
            // For our main loop, in my case, I opted to add
            // some functionality in that slept the loop for
            // 5 minutes if it found that it was charing. This
            // way I wasn't using up "unnecessary" CPU cycles
            //
            // Of course, all you need to call is StopWhining(); if
            // you want to keep the CPU busy for any length of time.
            while (true)
            {
                if (SystemInformation.PowerStatus.BatteryChargeStatus !=
                    BatteryChargeStatus.Charging &&
                    SystemInformation.PowerStatus.PowerLineStatus !=
                    PowerLineStatus.Online)
                {
                    StopWhining();
                }
                else
                {
                    // Sleep for 5 minutes since we are charing
                    // and most likely aren't hearing the 'whine'
                    Thread.Sleep(TimeSpan.FromMinutes(5.00));
                }
            }
        }
 
        /// <summary>
        /// This really does nothing except add
        /// 1000 values to an array and then adds
        /// another 1000 as it counts back down.
        /// </summary>
        private static void StopWhining()
        {
            double[] phone = new double[1000];
 
            for (int i = 0; i < 1000; i++)
            {
                phone[i] = 31337.666;
            }
 
            for (int i = 999; i >= 0; i--)
            {
                phone[i] = 666.31337;
            }
        }
    }
}

Revision: 36134
at November 18, 2010 15:54 by cabrel


Initial Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
 
namespace CpuWhiner
{
    class Program
    {
        //
        // To enable this to be 'user friendly' at start-up. We need to
        // suppress the console window.
        //
        // The following two methods will help us achieve this
        // FindWindow MSDN
        // http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
        //
        // ShowWindow MSDN
        // http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx
        //
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName,
                                                string lpWindowName);
 
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 
        static void Main(string[] args)
        {
            // Give our window a unique title
            Console.Title = "CpuWhinerConsoleWindow";
 
            // Create our pointer to our console window
            // and if the window was found, hide it
            IntPtr hWnd = FindWindow(null, Console.Title);
            if (hWnd != IntPtr.Zero)
            {
                // Hide the console window
                ShowWindow(hWnd, 0);
            }
 
            // For our main loop, in my case, I opted to add
            // some functionality in that slept the loop for
            // 5 minutes if it found that it was charing. This
            // way I wasn't using up "unnecessary" CPU cycles
            //
            // Of course, all you need to call is StopWhining(); if
            // you want to keep the CPU busy for any length of time.
            while (true)
            {
                if (SystemInformation.PowerStatus.BatteryChargeStatus
                    != BatteryChargeStatus.Charging)
                {
                    StopWhining();
                }
                else
                {
                    // Sleep for 5 minutes since we are charing
                    // and most likely aren't hearing the 'whine'
                    Thread.Sleep(TimeSpan.FromMinutes(5.00));
                }
            }
        }
 
        /// <summary>
        /// This really does nothing except add
        /// 1000 values to an array and then adds
        /// another 1000 as it counts back down.
        /// </summary>
        private static void StopWhining()
        {
            double[] phone = new double[1000];
 
            for (int i = 0; i < 1000; i++)
            {
                phone[i] = 31337.666;
            }
 
            for (int i = 999; i >= 0; i--)
            {
                phone[i] = 666.31337;
            }
        }
    }
}

Initial URL
http://blog.zerklabs.com/2010/11/would-you-like-some-c-sharp-cheese-with-that-cpu-whine/

Initial Description

                                

Initial Title
Solving CPU Whine

Initial Tags
windows, c#

Initial Language
C#