我想創建一個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