how to terminate a sub process along with main application

pmk2000

Member
Joined
Mar 31, 2005
Messages
6
Programming Experience
1-3
hi all

i created two applications
from second application i am running first application as a sub process
using process class

when i am closing my second application(i,e main application) i am not
able to terminate sub process along with it.

how can i terminate
plz help
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
perhaps this will help:

VB.NET:
Private mstrMySubProcessName as String 'This will be the exact name of your sub process
Call KillProcess(mstrMySubProcessName)

	Friend Sub KillProcess(ByVal ProcessName As String)
		Dim prc() As Process
		Dim ERROR_FILE_NOT_FOUND As Integer = 2
		Dim ERROR_ACCESS_DENIED As Integer = 5
		Try
			prc = System.Diagnostics.Process.GetProcessesByName(ProcessName)
			Dim eprc As IEnumerator = prc.GetEnumerator
			eprc.Reset()

			While eprc.MoveNext
			    Dim proc As Process = CType(eprc.Current, Process)
				proc.Kill()
				proc = Nothing
			End While

			eprc = Nothing
			prc = Nothing

		Catch e As System.ComponentModel.Win32Exception
			If e.NativeErrorCode = ERROR_FILE_NOT_FOUND Then
			    'MessageBox.Show(e.Message + ". Process not found.")
			    Throw New UnauthorizedAccessException(e.Message & ". Process not found.")
			Else
			    If e.NativeErrorCode = ERROR_ACCESS_DENIED Then
				    ' Note that if your word processor might generate exceptions 
				    ' such as this, which are handled first. 
				    'MessageBox.Show(e.Message + ". You do not have permission to kill this process.")
				    Throw New UnauthorizedAccessException(e.Message & ". You do not have permission to kill this process.")
				End If
			End If
		End Try
	End Sub
note this will kill ALL instances of your sub process so if you're app is open twice (each running their own sub process) and you close 1 copy of your app, you just killed both copies of the subprocess so be careful how you use this
 
Top Bottom