如何显示来自 Windows 服务的通知区域气球和图标?
- 作者: 我冷但不高
- 来源: 51数据库
- 2022-10-20
问题描述
我有一个 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.
希望这会有所帮助.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
