简体   繁体   English

C#WinForms:创建后台通知应用程序

[英]C# WinForms: Creating a background notification application

For practical purposes but also as a learning exercise, I am wanting to create notification application to remind me of friend's Birthdays. 出于实际目的,也作为学习练习,我想创建一个通知应用程序,以提醒我朋友的生日。 I would like the application to be running in the background so that it can check the current date against user configured reminders. 我希望该应用程序在后台运行,以便可以根据用户配置的提醒检查当前日期。 If a reminder is due then the user is displayed a simple Window that will allow them to snooze or dismiss the notification. 如果有提醒,则将向用户显示一个简单的窗口,该窗口将允许他们暂停或取消该通知。 I want the application to have user specific notifications (ie User A's notifications are not seen when User B is logged in). 我希望该应用程序具有特定于用户的通知(即,用户B登录时看不到用户A的通知)。

Option 1 - Application launched upon Windows startup: I thought about keeping it simple by checking when to display notifications only when the application starts up. 选项1-Windows启动时启动的应用程序:我想通过检查何时仅在应用程序启动时显示通知来简化操作。 I would then put a shortcut in the Windows Startup folder. 然后,我将在Windows启动文件夹中放置一个快捷方式。 However, this probably wouldn't work if the PC is put to sleep and resumed each day. 但是,如果PC每天进入睡眠状态并恢复运行,则可能无法正常工作。

Option 2 - Application constantly running in background: I then thought about making the application so it minimises to an icon in the System Tray and thus would constantly be running in the background. 选项2-应用程序始终在后台运行:然后我考虑制作该应用程序,以便将其最小化到系统任务栏中的一个图标,从而使其始终在后台运行。 It would then periodically check when to display notifications. 然后它将定期检查何时显示通知。

Option 3 - Using Windows Service with application: I then thought about using a Windows Service to do the periodic checks as to when to display notifications. 选项3-将Windows Service与应用程序一起使用:然后,我考虑使用Windows Service进行有关何时显示通知的定期检查。 Obviously, a Windows Service cannot display UI notifications so it would need to use some other mechanism for notification (eg sending an email). 显然,Windows服务无法显示UI通知,因此它需要使用其他某种通知机制(例如,发送电子邮件)。 An application could then be used to configure settings that the service uses. 然后可以使用应用程序来配置服务使用的设置。

Are there any other options that I have not considered which would work better. 我还有没有其他更好的选择。 I'm currently inclined to go with option 2 for my first implementation but could look to move to a better solution afterwards. 我目前倾向于在第一个实现中使用选项2,但之后可能会寻求一个更好的解决方案。

TIA TIA

Your option 2 is the optimal one since you are aimed to distinct users and their reminders - your application runs in the context of a particular user, you already know which remainders need to be shown. 您的选项2是最佳选择,因为您的目的是针对不同的用户及其提醒-您的应用程序在特定用户的上下文中运行,因此您已经知道需要显示哪些余数。 Of course, you could go for the third option but this way is really need more time to code and the code itself would be more complex. 当然,您可以选择第三个选项,但是这种方式确实需要更多的时间来编写代码,并且代码本身会更加复杂。 I would go for the second option. 我会选择第二种选择。 As your applications is WinForms, for the periodical checks I would recommend you to use System.Windows.Forms.Timer class. 由于您的应用程序是WinForms,因此对于定期检查,我建议您使用System.Windows.Forms.Timer类。

By the way, showing an icon in the tray notification area does not make your application "run in the background", it is just a convenient way to show your application is up and running. 顺便说一句,在托盘通知区域中显示图标并不能使您的应用程序“在后台运行”,这只是显示应用程序已启动并正在运行的一种简便方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM