Results 1 to 2 of 2

Thread: Closing an External Process

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Closing an External Process

    C# version here.

    You can use the .NET Process class to close an external application. First you must create a Process object that represents the application. You can then call Kill on that Process to force the application to close. That's the only way to close applications that don't have a GUI but, for those that do, it's better to call CloseMainWindow first and only call Kill as a last resort. It's like the difference between just throwing someone out and asking them to leave first. CloseMainWindow gives the application a chance to clean up before exiting, while Kill just removes it from memory. E.g.
    vb.net Code:
    1. Private Sub CloseProcessesByName(ByVal processName As String)
    2.     For Each p As Process In Process.GetProcessesByName(processName)
    3.         'Ask nicely for the process to close.
    4.         p.CloseMainWindow()
    5.  
    6.         'Wait up to 10 seconds for the process to close.
    7.         p.WaitForExit(10000)
    8.  
    9.         If Not p.HasExited Then
    10.             'The process did not close itself so force it to close.
    11.             p.Kill()
    12.         End If
    13.  
    14.         'Dispose the Process object, which is different to closing the running process.
    15.         p.Close()
    16.     Next
    17. End Sub
    The process name is what gets displayed in Windows Task Manager.
    Last edited by jmcilhinney; Dec 19th, 2010 at 09:34 AM. Reason: Added link to C# version.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: Closing an External Process

    This code is usefully if your application output value is full path.
    Vb.net Code:
    1. Try
    2.             Dim filePath As String = "C:\Users\user\Desktop\appname.exe"
    3.             Dim folderPath As String = IO.Path.GetFullPath(filePath)
    4.             Dim Namer As String = IO.Path.GetFileNameWithoutExtension(filePath)
    5.             For Each p As Process In Process.GetProcessesByName(Namer)
    6.                 p.CloseMainWindow()
    7.                 p.WaitForExit(10000)
    8.                 If Not p.HasExited Then
    9.                     p.Kill()
    10.                 End If
    11.             Next
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message)
    14.         End Try
    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

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