我希望创建一个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);
}
}
}
我仍然没有听到任何“哔”的声音。