views:

267

answers:

6

Hi,

The application should kill itself How to do in vb.net? Please Help me in this case...

+1  A: 

You can use Application.Exit function if you are doing a winforms application.

Like for example if you want to exit(kill) your application on click of a button

Sub button1_Click(sender As object, e As System.EventArgs)
   Application.Exit()
End Sub
Pradeep
+1  A: 

Application.Exit() should do the trick (or something similar, not got VS in fromt of me ATM)

this is a windows forms application right?

Kragen
+1  A: 

If its a Windows Forms application Application.Exit should do the trick.

Mark
+2  A: 

If it is a windows forms application the "cleanest" way is to close the main form (for instance by calling Me.Close() in it).

If closing the main form does not exit the application, it is likely that you have additional threads (that are not background threads) that are still running. If that is the case you should investigate why the threads are still running (or mark them as background threads when they are launched, so that they don't keep the application alive).

Fredrik Mörk
A: 

The really ugly way would be to use the system command:

taskkill.exe /f /im myapplication.exe

lungic
A: 
using System.Diagnostics;

/// <summary>
/// Kill all instances of a Process by name
/// </summary>
/// <param name="name">name of the process to kill</param>
/// <example>
/// KillAllProcesses( "Outlook" );
/// </example>
private void KillAllProcesses( string name )
{
    Process[] processes = Process.GetProcessesByName( name );
    foreach( Process p in processes )
        p.Kill();
}

this is for killing other processes (not itself). Code is on C# but I think you will be able to translate to VB.NET.

Also I will vote for Form.Close() as the most correct way to close the application, however sometimes it will require substantial design efforts (for example when you want to close the application when seriuos exception occurs in data layer, and data layer have no means to inform main form about exception) and Application.Exit is often considered as an easiest way...

p.s. Also, for a console application you should use

Environment.Exit()
Bogdan_Ch