Windows服务:OnPowerEvent不会触发

时间:2010-10-02 02:17:49

标签: c# .net windows-services service

我想创建一个Windows服务来跟踪是否插入了A / C电源适配器。为此,我正在尝试构建Windows服务,如下所示:

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

我仍然没有听到任何哔声。

2 个答案:

答案 0 :(得分:7)

除了覆盖方法的错误语法之外,您似乎没有设置CanHandlePowerEvent属性。

  

当电脑电源状态   更改,服务控制管理器   (SCM)验证是否提供服务   使用。接受电源事件命令   CanHandlePowerEvent的值。

     

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

请关于方法的一件事:

  
    

64位版本的Windows Vista和Windows XP不支持Beep方法。

  

答案 1 :(得分:4)

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

查看您最喜爱的C#编程书,了解 new 关键字。简要版本:它隐藏了基本方法,它不会覆盖它。因此它永远不会被召唤。删除新虚拟,使用覆盖,就像您对其他方法所做的那样。您还必须在构造函数中将CanHandlePowerEvent属性设置为true。

相关问题