除了抛出错误之外,我可以通过其他方式在执行过程中停止程序吗?
是否有命令可以停止我的程序的执行?
我有一项服务,每 10 分钟通过 telnet 处理一个交换帐户。在执行的一个点期间,当文件夹中没有电子邮件时,应用程序可能会从 telnet 会话获得响应,这看起来像这样:
* 0 EXISTS
所以因为这确实不是一个错误,我不想抛出错误 - 我只想在此时停止程序。对于我来说,也没有一个真正可以进行某种计数比较的好地方 - 无论如何,至少要重写大量代码,所以 If count <; 0 Then ...
语句在这种情况下实际上不起作用恕我直言。
那么我是否可以在此时停止我的申请,因为如果帐户中没有电子邮件就没有继续下去的意义?
MORE INFO: The response from telnet is generated in a class library. The program is not running in a winform or a console app. A Windows Service calls a method from my utilities class which starts the execution by making calls to other classes in my project. This response is generated in one of the classes which was started by the utilities class.
Is there a command that will stop the execution of my program?
I have a service that is processes an exchange account via telnet every 10 minutes. During one point of execution the application could possibly have a response from the telnet session when there are NO e-mails in the folder, which would look something like this:
* 0 EXISTS
so since this really isn't an error I don't want to throw an error - I just want to stop the program at that point. There isn't really a good place for me to do some sort of count comparison either - not without rewriting a lot of code anyway so an If count < 0 Then ...
statement would not really work in this case IMHO.
So is it possible for me to just stop my application at that point since there's no point in continuing if there are no e-mails in the account?
MORE INFO: The response from telnet is generated in a class library. The program is not running in a winform or a console app. A Windows Service calls a method from my utilities class which starts the execution by making calls to other classes in my project. This response is generated in one of the classes which was started by the utilities class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Environment.Exit - 终止此进程并给出底层操作系统指定的退出代码。
Environment.FailFast - 终止进程但不执行任何活动尝试-finally 块或终结器。
Environment.Exit - Terminates this process and gives the underlying operating system the specified exit code.
Environment.FailFast - Terminates a process but does not execute any active try-finally blocks or finalizers.
如果这是一个 winforms 应用程序,Application.Exit 应该可以工作。如果这是一个控制台应用程序,那么您需要从
Main
方法中return;
,尽管正确获取可能需要一些工作,具体取决于程序的设置方式。If this is a winforms app, Application.Exit should work. If this is a console application, then you want to
return;
from theMain
method, though properly getting there may be some work, depending on how your program is setup.如果这是一个控制台应用程序,并且您只是在 Main 方法中执行此操作,则只需在此处放置一个 return 即可退出应用程序。
If this is a console application and you are just doing this in the Main method, you can simply put a return there and it will exit the application.
永久停止应用程序?
关闭主窗体:
暂时停止几分钟?
使用计时器。
Stop the application permanently?
Close the main form:
Stop it temporarily for a few minutes?
Use a timer.
如果您使用线程从 Exchange 中读取数据,请确保通过让线程过程到达末尾来正确完成线程。
这通常是通过使用线程可访问的布尔变量来实现的,检查该变量并退出线程函数。
And if you are reading from Exchange using a thread, be sure to properly finish the thread by letting the thread procedure reach its end.
This is usually achieved by using a Boolean variable that is accessible by your thread, check for that variable and exit the thread function.
在您的服务中,您可以处理 OnCustomCommand 事件,然后让执行 telnet 处理的类通过 System.ServiceProcess.ServiceController 类的 ExecuteCommand 方法向服务发送命令。这个特殊命令基本上会告诉服务自行停止,这也可以由 ServiceController 对象来完成。
在您的班级中:
在您的服务中:
您可以使用 128 及以上的自定义命令。
In your service you can handle the OnCustomCommand event, then have your class that does the telnet processing send a command to the service via the ExecuteCommand method of the System.ServiceProcess.ServiceController class. This special command would basically tell the service to stop itself, which can be also be done by the ServiceController object.
From your class:
Within your service:
You can use custom commands from 128 and up.
VB End 语句将停止执行。但是,它不会执行 Dispose 或 Finalize 函数。我认为关闭主窗体和/或退出子主窗体被认为是更好的方式。 Application.Exit 也将终止 vb.net 程序,但仅在一些消息处理之后。
The VB End statement will stop execution. However, it won't execute Dispose or Finalize functions. I think it's considered better manners to close the main form and/or exit the Sub Main. Application.Exit will also terminate vb.net program, but only after some message processing.