关闭基本表单后,保持 VB.NET 应用程序在后台运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16702487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep VB.NET application running in the background after base form closed
提问by andrew.cuthbert
I am writing an application in VB.Net in which a user can schedule emails to be sent in the future. Is there a way to keep my listener thread running in the background after the base form on the application is closed? I would also need to start up the listener when the system boots, but wouldn't want any forms to open at that time.
我正在 VB.Net 中编写一个应用程序,用户可以在其中安排将来发送的电子邮件。在应用程序上的基本表单关闭后,有没有办法让我的侦听器线程在后台运行?我还需要在系统启动时启动侦听器,但当时不希望打开任何表单。
Example (desired) functionality: I open the application in the morning and schedule three emails, one of which should be sent in three hours, and the other two of which should be sent tomorrow morning. I close the application. In three hours, the first one sends. At the end of the day, I shut down my machine. When I turn it back on tomorrow morning, the other two are sent without me ever opening the application.
示例(所需)功能:我在早上打开应用程序并安排三封电子邮件,其中一封应在三小时内发送,另外两封应在明天早上发送。我关闭应用程序。三小时后,第一个发送。在一天结束时,我关闭了我的机器。当我明天早上重新打开它时,另外两个在我没有打开应用程序的情况下被发送。
I am thinking I need to separate the listener into a service, but want to make sure there isn't an easier way before going down that road.
我想我需要将侦听器分成一个服务,但要确保在走这条路之前没有更简单的方法。
If I do end up having a separate service and application, can I create an installer that will install both at once?
如果我最终拥有单独的服务和应用程序,是否可以创建一个安装程序来同时安装它们?
回答by Konrad Rudolph
In the options of your project, set the “Startup Object” to “Sub Main”instead of of a form name. You might have to disable the setting “Application Framework” first.
在项目的选项中,将“启动对象”设置为“Sub Main”而不是表单名称。您可能必须先禁用“应用程序框架”设置。
Now you have to launch the form manually because it's no longer done automatically, but on the other hand you can now controlwhen to launch it, and your application will exit when it reaches the end of the Mainmethod (or when it's quit explicitly) rather than when the form closes.
现在您必须手动启动表单,因为它不再自动完成,但另一方面,您现在可以控制何时启动它,并且您的应用程序将在到达Main方法结束时(或显式退出时)退出比当表单关闭时。
回答by Rob Perkins
If all you want is to keep the program running, you could supply it with the definition for a TaskBar icon, and represent state in your application using the icon's graphic. It would appear over on the right next to the system clock.
如果您只想保持程序运行,您可以为它提供任务栏图标的定义,并使用图标的图形表示应用程序中的状态。它会出现在系统时钟的右侧。
Then it's just a matter of reconfiguring your development project so that the application close event is triggered off that icon's menu instead of off the fact that the form is closed.
然后只需重新配置您的开发项目,以便应用程序关闭事件从该图标的菜单中触发,而不是关闭表单这一事实。
回答by Adhemar
Use a NotifyIcon control and when the user close the mainform capture the OnClosing Event to Ask the Question if he want to minimize to the tray or exit the application.
使用 NotifyIcon 控件,当用户关闭主窗体时捕获 OnClosing 事件以询问问题,如果他想最小化到托盘或退出应用程序。
Here is the documentation of NotifyIcon control from Microsoft:
这是 Microsoft 的 NotifyIcon 控件文档:
回答by FraserOfSmeg
I have a possible solution for you here. In the form closing event you can place the following code:
我在这里为您提供了一个可能的解决方案。在表单关闭事件中,您可以放置以下代码:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.Visible = False
e.Cancel = True
End Sub
This will hide the form and allow it to run in the background until you shutdown the computer. To handle the restart and then send emails I'd suggest adding the emails to be sent to a text file that can be accessed after restart and then adding the program to startup. An even better solution might be to have 2 programs, one that is on startup and is always hidden that checks the text file every 15 minutes (for example) and a second program that is launched when a new email is to be added to the list and it appends the text file with new emails to be sent.
这将隐藏表单并允许它在后台运行,直到您关闭计算机。要处理重新启动然后发送电子邮件,我建议将要发送的电子邮件添加到重新启动后可以访问的文本文件中,然后将程序添加到启动中。更好的解决方案可能是拥有 2 个程序,一个在启动时始终隐藏,每 15 分钟检查一次文本文件(例如),另一个程序在将新电子邮件添加到列表中时启动并将要发送的新电子邮件附加到文本文件中。
回答by imRen
(Sorry if i am wrong, just proposing a probable solution here) I think attaching a simple db to hold the email sending schedule information/date-time and loading the application at startup with Notification Icon (or without Notification Icon but with a shortcut to show user interface/form to feed the emails and schedule information/date-time, if required + BackgroundWorker, if you want the application to go to sleep mode and wake it later to send first scheduled email; after X hours). After sending first email update the db then exit the application. Check the db on startup to send the first email or go to sleep mode if pc boots before sending first email. But, if first email is already sent then send other two scheduled emails (check db date-time). This should do the job... :)
(对不起,如果我错了,只是在这里提出一个可能的解决方案)我认为附加一个简单的数据库来保存发送计划信息/日期时间的电子邮件,并在启动时使用通知图标(或没有通知图标,但有一个快捷方式)加载应用程序显示用户界面/表单以提供电子邮件和计划信息/日期时间(如果需要)+ BackgroundWorker,如果您希望应用程序进入睡眠模式并稍后唤醒它以发送第一个计划的电子邮件(X 小时后)。发送第一封电子邮件后更新数据库然后退出应用程序。在启动时检查数据库以发送第一封电子邮件,或者如果 PC 在发送第一封电子邮件之前启动,则进入睡眠模式。但是,如果第一封电子邮件已经发送,则发送另外两封预定的电子邮件(检查数据库日期时间)。这应该可以完成工作...... :)

