Posted By

cabrel on 11/18/10


Tagged

windows console CPU application c# hiding


Versions (?)

Solving CPU Whine


 / Published in: C#
 

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Runtime.InteropServices;
  5. using System.Threading;
  6. using System.Windows.Forms;
  7.  
  8. namespace CpuWhiner
  9. {
  10. class Program
  11. {
  12. //
  13. // To enable this to be 'user friendly' at start-up. We need to
  14. // suppress the console window.
  15. //
  16. // The following two methods will help us achieve this
  17. // FindWindow MSDN
  18. // http://msdn.microsoft.com/en-us/library/ms633499(VS.85).aspx
  19. //
  20. // ShowWindow MSDN
  21. // http://msdn.microsoft.com/en-us/library/ms633548(VS.85).aspx
  22. //
  23. [DllImport("user32.dll")]
  24. public static extern IntPtr FindWindow(string lpClassName,
  25. string lpWindowName);
  26.  
  27. [DllImport("user32.dll")]
  28. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  29.  
  30. static void Main(string[] args)
  31. {
  32. // Give our window a unique title
  33. Console.Title = "CpuWhinerConsoleWindow";
  34.  
  35. // Create our pointer to our console window
  36. // and if the window was found, hide it
  37. IntPtr hWnd = FindWindow(null, Console.Title);
  38. if (hWnd != IntPtr.Zero)
  39. {
  40. // Hide the console window
  41. ShowWindow(hWnd, 0);
  42. }
  43.  
  44. // For our main loop, in my case, I opted to add
  45. // some functionality in that slept the loop for
  46. // 5 minutes if it found that it was charing. This
  47. // way I wasn't using up "unnecessary" CPU cycles
  48. //
  49. // Of course, all you need to call is StopWhining(); if
  50. // you want to keep the CPU busy for any length of time.
  51. while (true)
  52. {
  53. if (SystemInformation.PowerStatus.BatteryChargeStatus !=
  54. BatteryChargeStatus.Charging &&
  55. SystemInformation.PowerStatus.PowerLineStatus !=
  56. PowerLineStatus.Online)
  57. {
  58. StopWhining();
  59. }
  60. else
  61. {
  62. // Sleep for 5 minutes since we are charing
  63. // and most likely aren't hearing the 'whine'
  64. Thread.Sleep(TimeSpan.FromMinutes(5.00));
  65. }
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// This really does nothing except add
  71. /// 1000 values to an array and then adds
  72. /// another 1000 as it counts back down.
  73. /// </summary>
  74. private static void StopWhining()
  75. {
  76. double[] phone = new double[1000];
  77.  
  78. for (int i = 0; i < 1000; i++)
  79. {
  80. phone[i] = 31337.666;
  81. }
  82.  
  83. for (int i = 999; i >= 0; i--)
  84. {
  85. phone[i] = 666.31337;
  86. }
  87. }
  88. }
  89. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: cabrel on November 18, 2010

Idea adapted from: http://www.reddit.com/r/programming/comments/e4ecd/themostusefulprogramiveeverwritten_does/

You need to login to post a comment.