我有一个 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模板网!