【发布时间】: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