title: Windows Service: OnPowerEvent doesn't fire
body: <p>I want to create a Windows Service that tracks if the A/C power adapter is plugged in or not. For that, I am trying to build Windows Service as below:</p>
  
  <pre><code>using System;
  using System.ServiceProcess;
  
  namespace PowerAlert {
      partial class PowerAlert : ServiceBase {
          public PowerAlert() {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args) {
              base.OnStart(args);
          }
  
          protected override void OnStop() {
              base.OnStop();
          }
  
          protected new virtual void OnPowerEvent(PowerBroadcastStatus powerStatus) {
              Console.Beep();
          }
      }
  }
  </code></pre>
  
  <p>After installing the service and starting it from services.msc, when I unplug my adapter and plug it back in, I don't hear the beep.</p>
  
  <p>I am sure I am not doing something correctly. How can I identify what that is?</p>
  
  <h2>Edit 1</h2>
  
  <p>As you can see from below, CanHandlePowerEvent is set to true.</p>
  
  <pre><code>namespace PowerAlert {
      partial class PowerAlert {
          /// &lt;summary&gt; 
          /// Required designer variable.
          /// &lt;/summary&gt;
          private System.ComponentModel.IContainer components = null;
  
          /// &lt;summary&gt;
          /// Clean up any resources being used.
          /// &lt;/summary&gt;
          /// &lt;param name="disposing"&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;
          protected override void Dispose(bool disposing) {
              if (disposing &amp;&amp; (components != null)) {
                  components.Dispose();
              }
              base.Dispose(disposing);
          }
  
          #region Component Designer generated code
  
          /// &lt;summary&gt; 
          /// Required method for Designer support - do not modify 
          /// the contents of this method with the code editor.
          /// &lt;/summary&gt;
          private void InitializeComponent() {
              // 
              // PowerAlert
              // 
              this.CanHandlePowerEvent = true;
              this.ServiceName = "PowerAlert";
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>I have overridden the OnPowerEvent as below:</p>
  
  <pre><code>using System;
  using System.ServiceProcess;
  
  namespace PowerAlert{
      partial class PowerAlert: ServiceBase {
          public PowerAlert() {
              InitializeComponent();
          }
  
          protected override void OnStart(string[] args) {
              base.OnStart(args);
          }
  
          protected override void OnStop() {
              base.OnStop();
          }
  
          protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) {
              Console.Beep();
  
              return base.OnPowerEvent(powerStatus);
          }
      }
  }
  </code></pre>
  
  <p>Still I don't hear any beep.</p>