Get the system process and end a specified process (C #)

Source: Internet
Author: User
Tags foreach tostring
The process itself thought of another way to do

To modify the registry first:
Hkey_local_machine\\system\\currentcontrolset\\service\\perfproc\\performance
Under: Disable performance counters is changed to 0

Get the current process of the system and add it to the listbox:
system.diagnostics.process[] Processoncomputer = System.Diagnostics.Process.GetProcesses ();
foreach (System.Diagnostics.Process p in Processoncomputer)
{
THIS.LISTBOX1.ITEMS.ADD (P.processname);
}

To close a specified process:
system.diagnostics.process[] Process = System.Diagnostics.Process.GetProcessesByName ( This.listBox1.SelectedItem.ToString ());

foreach (System.Diagnostics.Process p in Process)
{
P.kill ();
}

However, this will close all instances of the process, such as if you open more than one ie, all the IE windows will be closed.

The following implementation closes a specific instance of IE
First declare a ArrayList:
ArrayList windowhandle = new ArrayList ();

Gets all instances of the specified process, puts it in a ListBox, and places the main window's handle in the ArrayList:
system.diagnostics.process[] Processoncomputer = System.Diagnostics.Process.GetProcessesByName ( This.listBox1.SelectedItem.ToString ());
foreach (System.Diagnostics.Process p in Processoncomputer)
{
THIS.LISTBOX2.ITEMS.ADD (p.mainwindowtitle);//Display the title of the main form in the listbox
Windowhandle.add (P.mainwindowhandle);
}

The handle and ArrayList of the main window of the specified process are turned off if they are met
system.diagnostics.process[] Process = System.Diagnostics.Process.GetProcessesByName ( This.listBox1.SelectedItem.ToString ());
foreach (System.Diagnostics.Process p in Process)
{
if (P.mainwindowhandle = = (System.IntPtr) (Windowhandle[this.listbox2.selectedindex))
{
P.kill ();
This.listBox2.Items.RemoveAt (This.listBox2.SelectedIndex);
}
}

This will close the process with the main form, but not the main form.
And please give us a lot of advice




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.