views:

513

answers:

8

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.

+2  A: 

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.

Timothy Carter
+1  A: 

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;
Max Schmeling
A: 

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.

Beth
This works if the startup object is a form, but may not if the startup object is Sub Main. You might have to exit from Sub Main, if that doesn't automatically happen in the code.
xpda
Yes, but those solutions for a console app are already listed. I responded before he added more detail (not winforms or console.)
Beth
A: 

It depends where you are in the program itself. If you're in the main function, you can just call a "return"... If not, then you could use System.Diagnostics to do it.

The following is C# code, but you can modify it for VB.NET:

using System.Diagnostics;

static void KillProgram(string processName)
{
    foreach (Process singleProcess in Process.GetProcessesByName(processName)
        singleProcess.Kill();
}

processName would be the name of the executable, omitting the .exe part. Obviously, just calling a return statement would be preferable in this case, but it depends how much code has already been written, and how far in the procedural stack you are.

Breakthrough
A: 

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.

Pierre-Alain Vigeant
A: 

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.

Chris Tybur
A: 

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.

xpda
A: 

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.

Simon Svensson