Skip to content

Instantly share code, notes, and snippets.

@cabrel
Created February 24, 2013 21:45
Show Gist options
  • Save cabrel/5025808 to your computer and use it in GitHub Desktop.
Save cabrel/5025808 to your computer and use it in GitHub Desktop.
Some cheese to go with that (CPU) W(h)ine?
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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment