【问题标题】:Windows Service: OnPowerEvent doesn't fireWindows 服务:OnPowerEvent 不触发
【发布时间】:2010-10-02 02:17:48
【问题描述】:

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

我还是没有听到任何哔哔声。

【问题讨论】:

  • 它可以作为一个独立的程序工作吗?
  • 你确定你有扬声器吗? :D
  • @Henrik P. Hessel:哈哈,是的,我有扬声器!
  • 执行 Console.Beep();方法独立工作?

标签: c# .net windows-services service


【解决方案1】:

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

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

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

关于方法的一件事:

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

【讨论】:

  • 宾果游戏!我使用的是 64 位 Windows 7。让我尝试写入日志文件。
【解决方案2】:
    protected new virtual void OnPowerEvent(PowerBroadcastStatus powerStatus) {
        Console.Beep();
    }

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

【讨论】:

    猜你喜欢
    • 2016-05-29
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多