問題描述
我有一個 Windows 服務,當用戶啟動他們的工作站時它總是在運行.此 Windows 服務很關鍵,我想在發生某些事情(例如服務停止、啟動、重新啟動等)時在通知區域顯示氣球通知.
I have a Windows Service that is always running when the user starts their workstation. This Windows Service is critical and I would like to show a Balloon Notification in the Notification Area when certain things happen such as the Service Stops, Starts, Restarts etc.
例如:
另外,有沒有辦法為我的 Windows 服務顯示通知區域圖標?
Also, is there a way to show a Notification Area Icon for my Windows Service?
推薦答案
Windows 服務直接與桌面交互的時代已經結束,因此您必須尋找另一種方式.
The days of Windows services interacting directly with the desktop are over, so you have to find another way.
我所做的是創建一個包含 NotifyIcon 的普通 WinForms 應用程序.此應用程序的行為模仿任務管理器的行為,因此它可以從任務欄中隱藏,并且只能在系統托盤中可見.如果我右鍵單擊系統托盤圖標,我會看到一個菜單.如果我雙擊該圖標,則會顯示應用程序窗口.
What I have done is create a normal WinForms application that includes a NotifyIcon. The behavior of this application mimics that of Task Manager, such that it can be hidden from the task bar and only visible in the system tray. If I right-click the system tray icon, I get a menu. If I double-click the icon, the application window is shown.
為了方便WinForms應用程序和Windows服務之間的通信,我使用了WCF.具體來說,我使用 Juval Lowy 的 發布訂閱框架,它非常適合這種場景.查看我的回答此處了解更多信息詳情.
To facilitate the communication between the WinForms application and the Windows service, I use WCF. Specifically, I use Juval Lowy's Publish-Subscribe Framework, which works really well for this kind of scenario. See my answer here for more details.
希望這會有所幫助.
這篇關于如何顯示來自 Windows 服務的通知區域氣球和圖標?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!