Hi Ashu,
Try the below C# code its self explanatory.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;
namespace ProcessSampleCS
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnKill;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.btnKill = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(128, 80);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnKill
//
this.btnKill.Location = new System.Drawing.Point(128, 144);
this.btnKill.Name = "btnKill";
this.btnKill.TabIndex = 1;
this.btnKill.Text = "Kill";
this.btnKill.Click += new System.EventHandler(this.btnKill_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.btnKill);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
int intProcessId = 0;
private void btnStart_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process objProcess = new System.Diagnostics.Process();
objProcess.EnableRaisingEvents = true;
objProcess.StartInfo.FileName = "Notepad.exe";
objProcess.Start();
intProcessId = objProcess.Id;
objProcess.Exited +=new EventHandler(objProcess_Exited);
}
private void objProcess_Exited(object sender, EventArgs e)
{
System.Diagnostics.Process objExitedProcess;
objExitedProcess = (System.Diagnostics.Process) sender;
MessageBox.Show("Application Closed with process ID " + objExitedProcess.Id.ToString());
}
private void btnKill_Click(object sender, System.EventArgs e)
{
System.Diagnostics.Process objProcess = System.Diagnostics.Process.GetProcessById(intProce ssId);
objProcess.Kill();
}
}
}
VB.NET Code
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Diagnostics
Namespace ProcessSampleCS
Public Class Form1
Inherits System.Windows.Forms.Form
Private btnStart As System.Windows.Forms.Button
Private btnKill As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Sub InitializeComponent()
Me.btnStart = New System.Windows.Forms.Button
Me.btnKill = New System.Windows.Forms.Button
Me.SuspendLayout()
Me.btnStart.Location = New System.Drawing.Point(128, 80)
Me.btnStart.Name = "btnStart"
Me.btnStart.TabIndex = 0
Me.btnStart.Text = "Start"
AddHandler Me.btnStart.Click, AddressOf Me.btnStart_Click
Me.btnKill.Location = New System.Drawing.Point(128, 144)
Me.btnKill.Name = "btnKill"
Me.btnKill.TabIndex = 1
Me.btnKill.Text = "Kill"
AddHandler Me.btnKill.Click, AddressOf Me.btnKill_Click
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.btnKill)
Me.Controls.Add(Me.btnStart)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
<STAThread()> _
Shared Sub Main()
Application.Run(New Form1)
End Sub
Private intProcessId As Integer = 0
Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim objProcess As System.Diagnostics.Process = New System.Diagnostics.Process
objProcess.EnableRaisingEvents = True
objProcess.StartInfo.FileName = "Notepad.exe"
objProcess.Start()
intProcessId = objProcess.Id
AddHandler objProcess.Exited, AddressOf objProcess_Exited
End Sub
Private Sub objProcess_Exited(ByVal sender As Object, ByVal e As EventArgs)
Dim objExitedProcess As System.Diagnostics.Process
objExitedProcess = CType(sender, System.Diagnostics.Process)
MessageBox.Show("Application Closed with process ID " + objExitedProcess.Id.ToString)
End Sub
Private Sub btnKill_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim objProcess As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessById(intProce ssId)
objProcess.Kill()
End Sub
End Class
End Namespace
Regards,
Karthik Simha
Regards,
Karthik Simha