Closing an External Process-VBForums
Results 1 to 6 of 6

Thread: Closing an External Process

  1. #1

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,600

    Closing an External Process

    VB 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.
    csharp Code:
    1. private void CloseProcessesByName(string processName)
    2. {
    3.     foreach (Process p in Process.GetProcessesByName(processName))
    4.     {
    5.         // Ask nicely for the process to close.
    6.         p.CloseMainWindow();
    7.  
    8.         // Wait up to 10 seconds for the process to close.
    9.         p.WaitForExit(10000);
    10.  
    11.         if (! p.HasExited)
    12.         {
    13.             // The process did not close itself so force it to close.
    14.             p.Kill();
    15.         }
    16.  
    17.         // Dispose the Process object, which is different to closing the running process.
    18.         p.Close();
    19.     }
    20. }
    The process name is what gets displayed in Windows Task Manager.

    NOTE: Code converted using Instant C# by Tangible Software Solutions.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    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 Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    Re: Closing an External Process

    That's nice clean and simple compared to say the lines and lines you used to need for VB6.

  3. #3
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    609

    Re: Closing an External Process

    Cool is it ok if i can use this cos i am going to make a process viewer.
    thanks for shareing.

  4. #4
    New Member
    Join Date
    Mar 2014
    Location
    Internet NetWork
    Posts
    8

    Re: Closing an External Process

    Ok , Thanks To share ...
    ..
    I'm try to kill some program .. but that's no work !!
    What's problem ?

  5. #5

    Thread Starter
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    96,600

    Re: Closing an External Process

    Quote Originally Posted by a_almisery View Post
    Ok , Thanks To share ...
    ..
    I'm try to kill some program .. but that's no work !!
    What's problem ?
    How could anyone answer that question based on the information you have provided?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Fanatic Member
    Join Date
    Jun 2006
    Posts
    615

    Re: Closing an External Process

    Quote Originally Posted by jmcilhinney View Post
    How could anyone answer that question based on the information you have provided?
    I'm pretty sure he's following your posts around and spamming them. He's the same one that commented on my thread that you just replied to.
    If ive helped, RATE my post.

    If your thread is solved, and you got your answer, mark this thread Resolved.

    There is no other forum like VBFORUMS.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.