問(wèn)題描述
我有一個(gè) Windows 服務(wù),當(dāng)用戶啟動(dòng)他們的工作站時(shí)它總是在運(yùn)行.此 Windows 服務(wù)很關(guān)鍵,我想在發(fā)生某些事情(例如服務(wù)停止、啟動(dòng)、重新啟動(dòng)等)時(shí)在通知區(qū)域顯示氣球通知.
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.
例如:
另外,有沒(méi)有辦法為我的 Windows 服務(wù)顯示通知區(qū)域圖標(biāo)?
Also, is there a way to show a Notification Area Icon for my Windows Service?
推薦答案
Windows 服務(wù)直接與桌面交互的時(shí)代已經(jīng)結(jié)束,因此您必須尋找另一種方式.
The days of Windows services interacting directly with the desktop are over, so you have to find another way.
我所做的是創(chuàng)建一個(gè)包含 NotifyIcon 的普通 WinForms 應(yīng)用程序.此應(yīng)用程序的行為模仿任務(wù)管理器的行為,因此它可以從任務(wù)欄中隱藏,并且只能在系統(tǒng)托盤(pán)中可見(jiàn).如果我右鍵單擊系統(tǒng)托盤(pán)圖標(biāo),我會(huì)看到一個(gè)菜單.如果我雙擊該圖標(biāo),則會(huì)顯示應(yīng)用程序窗口.
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應(yīng)用程序和Windows服務(wù)之間的通信,我使用了WCF.具體來(lái)說(shuō),我使用 Juval Lowy 的 發(fā)布訂閱框架,它非常適合這種場(chǎng)景.查看我的回答此處了解更多信息詳情.
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.
希望這會(huì)有所幫助.
這篇關(guān)于如何顯示來(lái)自 Windows 服務(wù)的通知區(qū)域氣球和圖標(biāo)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!