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

Source: Internet
Author: User
Get the current process of the system and add it to ListBox:
System. Diagnostics. Process [] processoncomputer = system. Diagnostics. process. getprocesses ();
Foreach (system. Diagnostics. Process P in processoncomputer)
{
This. listbox1.items. Add (P. processname );

}

 

Shut down 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. For example, if you open multiple ie instances, all IE Windows will be closed.

 

The following code disables a specific ie instance:
Declare An arraylist first:
Arraylist tables whandle = new arraylist ();

Get all the instances of the specified process, put them in a ListBox, and put the handle of the main window into 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 ListBox
Define whandle. Add (P. main1_whandle );
}

Compares the handle in the main window of the specified process with that in arraylist. If yes, It is disabled.
System. Diagnostics. Process [] process = system. Diagnostics. process. getprocessesbyname (this. listbox1.selecteditem. tostring ());
Foreach (system. Diagnostics. Process P in process)
{
If (P. main1_whandle = (system. intptr) (when whandle [This. listbox2.selectedindex])
{
P. Kill ();
This. listbox2.items. removeat (this. listbox2.selectedindex );
}
}

In this way, the process with the main form can be closed, but the process without the main form does not work.

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.