除了抛出错误之外,我可以通过其他方式在执行过程中停止程序吗?

发布于 2024-08-02 12:21:03 字数 749 浏览 1 评论 0原文

是否有命令可以停止我的程序的执行?

我有一项服务,每 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别再吹冷风 2024-08-09 12:21:04

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.

尐籹人 2024-08-09 12:21:04

如果这是一个 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 the Main method, though properly getting there may be some work, depending on how your program is setup.

她说她爱他 2024-08-09 12:21:04

如果这是一个控制台应用程序,并且您只是在 Main 方法中执行此操作,则只需在此处放置一个 return 即可退出应用程序。

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.

return;
维持三分热 2024-08-09 12:21:04

永久停止应用程序?
关闭主窗体:

  Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuFileExit.Click
     Me.Close()
  End Sub

暂时停止几分钟?
使用计时器。

Stop the application permanently?
Close the main form:

  Private Sub mnuFileExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuFileExit.Click
     Me.Close()
  End Sub

Stop it temporarily for a few minutes?
Use a timer.

妄断弥空 2024-08-09 12:21:04

如果您使用线程从 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.

囚我心虐我身 2024-08-09 12:21:04

在您的服务中,您可以处理 OnCustomCommand 事件,然后让执行 telnet 处理的类通过 System.ServiceProcess.ServiceController 类的 ExecuteCommand 方法向服务发送命令。这个特殊命令基本上会告诉服务自行停止,这也可以由 ServiceController 对象来完成。

在您的班级中:

Dim oServCtl As System.ServiceProcess.ServiceController
oServCtl.ServiceName = "MyServiceName"
oServCtl.ExecuteCommand(128)

在您的服务中:

Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
    Dim oServCtl As System.ServiceProcess.ServiceController

    If command = 128 Then
        oServCtl = New System.ServiceProcess.ServiceController
        oServCtl.ServiceName = "MyServiceName"
        oServCtl.Stop()
        oServCtl.Close()
    End If
End Sub

您可以使用 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:

Dim oServCtl As System.ServiceProcess.ServiceController
oServCtl.ServiceName = "MyServiceName"
oServCtl.ExecuteCommand(128)

Within your service:

Protected Overrides Sub OnCustomCommand(ByVal command As Integer)
    Dim oServCtl As System.ServiceProcess.ServiceController

    If command = 128 Then
        oServCtl = New System.ServiceProcess.ServiceController
        oServCtl.ServiceName = "MyServiceName"
        oServCtl.Stop()
        oServCtl.Close()
    End If
End Sub

You can use custom commands from 128 and up.

玩世 2024-08-09 12:21:04

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文