2010-10-02 39 views
2

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

不過我沒有聽到任何蜂鳴聲。

+0

它作爲一個獨立的程序工作嗎? – 2010-10-02 02:19:31

+1

你確定你有揚聲器嗎? :D – 2010-10-02 12:41:50

+0

@Henrik P. Hessel:哈哈是的,我確實有發言者! – Moon 2010-10-02 12:43:56

回答

7

除了覆蓋方法的錯誤語法外,它看起來像沒有設置CanHandlePowerEvent屬性。

當計算機電源狀態 變化,服務控制管理器 (SCM)驗證使用CanHandlePowerEvent的 值服務 是否接受電力事件的命令。

如果CanHandlePowerEvent爲真,則 命令被傳遞到服務,並且如果確定 的OnPowerEvent方法被調用。如果OnPowerEvent不是在派生類中實現的 ,則 SCM通過 處理電源事件的空基類 ServiceBase.OnPowerEvent method.ServiceBase.OnPowerEvent方法。

並請一件事的方法:不支持在Windows Vista和Windows XP的64位版本

的蜂鳴方法。

+0

賓果!我正在使用64位Windows 7.讓我嘗試寫入日誌文件。 – Moon 2010-10-03 02:06:24

4
protected new virtual void OnPowerEvent(PowerBroadcastStatus powerStatus) { 
     Console.Beep(); 
    } 

檢查你最喜歡的C#編程書介紹關鍵字。簡要版本:它隱藏了基本方法,它不覆蓋它。它將永遠不會被調用。刪除新的虛擬,使用覆蓋,就像你爲其他方法做的一樣。您還必須在構造函數中將CanHandlePowerEvent屬性設置爲true。

相關問題