Windows服务:OnPowerEvent不触发

3

我希望创建一个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);
        }
    }
}

我仍然没有听到任何“哔”的声音。


它能作为独立程序运行吗? - Preet Sangha
1
你确定你有扬声器吗? :D - Henrik P. Hessel
@Henrik P. Hessel:哈哈,是的,我有扬声器! - Moon
Console.Beep() 这个方法能独立地工作吗? - Henrik P. Hessel
2个回答

7
除了覆盖方法的错误语法之外,看起来您没有设置CanHandlePowerEvent属性。
当计算机电源状态更改时,服务控制管理器(SCM)使用CanHandlePowerEvent的值验证服务是否接受电源事件命令。
如果CanHandlePowerEvent为true,则将命令传递给服务,并调用OnPowerEvent方法(如果已定义)。 如果在派生类中未实现OnPowerEvent,则SCM通过空基类ServiceBase.OnPowerEvent方法处理电源事件。
还有一件事情需要注意:
64位版本的Windows Vista和Windows XP不支持Beep方法。

太好了!我正在使用64位的Windows 7。让我尝试写入日志文件。 - Moon

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

请查看您喜欢的C#编程书籍,了解关于new关键字的内容。简单来说,它可以隐藏基类方法,但不会覆盖它。因此,它永远不会被调用。使用override而不是new virtual,就像您为其他方法所做的一样。您还需要在构造函数中将CanHandlePowerEvent属性设置为true。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接