|
-
Mar 8th, 2011, 06:59 AM
#1
[VB2010] Start, Stop and Detect the closing of an external application (process)
Hi guys...
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:
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 13th, 2011, 10:42 PM
#2
Frenzied Member
Re: [VB2010] Start, Stop and Detect the closing of an external application (process)
Very nice code.
You said:
 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.
-
Mar 14th, 2011, 06:04 AM
#3
Re: [VB2010] Start, Stop and Detect the closing of an external application (process)
If my post was helpful to you, then express your gratitude using Rate this Post. 
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|