Results 1 to 3 of 3

Thread: [VB2010] Start, Stop and Detect the closing of an external application (process)

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Arrow [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:
    1. Public Class Form1
    2.     Dim myProcess As Process    '~~~ Our object that references the process we want to start
    3.  
    4.     '~~~ Start
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         myProcess = New Process                         '~~~ Creating the object
    7.         myProcess.StartInfo.FileName = "notepad.exe"    '~~~ We are going to start notepad.
    8.         myProcess.Start()                               '~~~ Start it
    9.  
    10.         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)
    11.         AddHandler myProcess.Exited, AddressOf ProcessExited    '~~~ When the process is exited, the sub "ProcessExited" will be called
    12.     End Sub
    13.  
    14.     '~~~ Stop
    15.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    16.         If myProcess IsNot Nothing Then             '~~~ Check if we have started the process or not
    17.             If myProcess.HasExited = False Then     '~~~ Check if the process is already exited or not
    18.                 myProcess.CloseMainWindow()         '~~~ If not, try to close it
    19.  
    20.                 myProcess.WaitForExit(1000)         '~~~ Wait for 1000 millisecconds allowing it to close
    21.  
    22.                 If myProcess.HasExited = False Then '~~~ If the process haven't closed yet (even after waiting), then we are going to force it to close
    23.                     myProcess.Kill()
    24.                 End If
    25.             End If
    26.         End If
    27.     End Sub
    28.  
    29.     '~~~ 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
    30.     Private Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
    31.         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)
    32.  
    33.         myProcess.Close()   '~~~ Freeup the object
    34.     End Sub
    35. 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,...

  2. #2
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    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.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [VB2010] Start, Stop and Detect the closing of an external application (process)

    Quote Originally Posted by Pc_Not_Mac View Post
    Very nice code.
    You said:

    What program could one be addicted to?
    For me its: Visual Studios, and the internet.
    Thanks

    It was just for a funny feeling
    I'm addicted to VB and VBForums (using Opera)


    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
  •  



Click Here to Expand Forum to Full Width