2010-10-02 122 views
2

我想创建一个Windows服务来跟踪A/C电源适配器是否插入。对于这一点,我想建立Windows服务如下:Windows服务:OnPowerEvent不会触发

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(); 
     } 
    } 
} 

安装服务,并从SERVICES.MSC启动它,当我拔掉我的适配器后,后插上它,我没有听到蜂鸣声。

我确定我没在做东西正确。你能帮我确定一下/那些东西吗?

编辑1

正如你可以从下面看到,CanHandlePowerEvent设置为true。

namespace PowerAlert { 
    partial class PowerAlert { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) { 
      if (disposing && (components != null)) { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Component Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() { 
      // 
      // PowerAlert 
      // 
      this.CanHandlePowerEvent = true; 
      this.ServiceName = "PowerAlert"; 
     } 

     #endregion 
    } 
} 

我已经覆盖了OnPowerEvent如下:

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); 
     } 
    } 
} 

不过我没有听到任何蜂鸣声。

+0

它作为一个独立的程序工作吗? – 2010-10-02 02:19:31

+1

你确定你有扬声器吗? :D – 2010-10-02 12:41:50

+0

@Henrik P. Hessel:哈哈是的,我确实有发言者! – Moon 2010-10-02 12:43:56

回答

7

除了覆盖方法的错误语法外,它看起来像没有设置CanHandlePowerEvent属性。

当计算机电源状态 变化,服务控制管理器 (SCM)验证使用CanHandlePowerEvent的 值服务 是否接受电力事件的命令。

如果CanHandlePowerEvent为真,则 命令被传递到服务,并且如果确定 的OnPowerEvent方法被调用。如果OnPowerEvent不是在派生类中实现的 ,则 SCM通过 处理电源事件的空基类 ServiceBase.OnPowerEvent method.ServiceBase.OnPowerEvent方法。

并请一件事的方法:不支持在Windows Vista和Windows XP的64位版本

的蜂鸣方法。

+0

宾果!我正在使用64位Windows 7.让我尝试写入日志文件。 – Moon 2010-10-03 02:06:24

4
protected new virtual void OnPowerEvent(PowerBroadcastStatus powerStatus) { 
     Console.Beep(); 
    } 

检查你最喜欢的C#编程书介绍关键字。简要版本:它隐藏了基本方法,它不覆盖它。它将永远不会被调用。删除新的虚拟,使用覆盖,就像你为其他方法做的一样。您还必须在构造函数中将CanHandlePowerEvent属性设置为true。

相关问题