我想创建一个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);
}
}
}
不过我没有听到任何蜂鸣声。
它作为一个独立的程序工作吗? – 2010-10-02 02:19:31
你确定你有扬声器吗? :D – 2010-10-02 12:41:50
@Henrik P. Hessel:哈哈是的,我确实有发言者! – Moon 2010-10-02 12:43:56