[VB2010] Start, Stop and Detect the closing of an external application (process)
Hi guys...:wave:
I was trying to find a way to detect the starting and stopping of an external application. So, I did a quick search and found the solution from here itself.
And I think, it would be better to post it here so that others could also make use of it.
vb.net Code:
Public Class Form1
Dim myProcess As Process '~~~ Our object that references the process we want to start
'~~~ Start
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myProcess = New Process '~~~ Creating the object
myProcess.StartInfo.FileName = "notepad.exe" '~~~ We are going to start notepad.
myProcess.Start() '~~~ Start it
myProcess.EnableRaisingEvents = True '~~~ Need to be TRUE, inorder to notify us when the process is closed by the user in some other means (like Alt + F4)
AddHandler myProcess.Exited, AddressOf ProcessExited '~~~ When the process is exited, the sub "ProcessExited" will be called
End Sub
'~~~ Stop
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If myProcess IsNot Nothing Then '~~~ Check if we have started the process or not
If myProcess.HasExited = False Then '~~~ Check if the process is already exited or not
myProcess.CloseMainWindow() '~~~ If not, try to close it
myProcess.WaitForExit(1000) '~~~ Wait for 1000 millisecconds allowing it to close
If myProcess.HasExited = False Then '~~~ If the process haven't closed yet (even after waiting), then we are going to force it to close
myProcess.Kill()
End If
End If
End If
End Sub
'~~~ This is the sub which will be called when the Process is closed. Whatever we want to do (when the process is closed), has to be included in this sub
Private Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("Hey dude, you were using this program for XX minutes ! Oh man !!! You are so addicted to this program...! You closed the program at: " & myProcess.ExitTime)
myProcess.Close() '~~~ Freeup the object
End Sub
End Class
Hope this helps... :)
Starting of the application will be done through our program. And when the user closes the program, it would notify you.
Thanks to:
:wave:
Re: [VB2010] Start, Stop and Detect the closing of an external application (process)
Very nice code.
You said:
Quote:
Originally Posted by akhileshbc
You are so addicted to this program.
What program could one be addicted to?
For me its: Visual Studios, and the internet.
Re: [VB2010] Start, Stop and Detect the closing of an external application (process)
Quote:
Originally Posted by
Pc_Not_Mac
Very nice code.
You said:
What program could one be addicted to?
For me its: Visual Studios, and the internet.
Thanks :wave:
It was just for a funny feeling :)
I'm addicted to VB and VBForums (using Opera) :D
:wave: